question

peterliles-4155 avatar image
0 Votes"
peterliles-4155 asked YijingSun-MSFT commented

Activate JQUERY Document ready function only on specific pages in global javascript file

I have a drag and drop feature that is activated when particular page is open using the document method. As i understand it is fired every time on page load. I have now placed the code in a javascript file. The trouble is it will now load on all pages that access javascript file when i just want it to load when particular pages load, two?

var srcElement = '';

$(document).ready(function () {

dotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered YijingSun-MSFT commented

Hi @peterliles-4155 ,
Why you don't put the javascript file in the specific pages instead of in the global? I suggest you could write the drag function in the javascript file and then you could reference the javascript file in the specific pages.

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have added the function to a external file and now reference it from a page like below. The problem that arises is that other pages also reference this external file and therefore cause the function to load everytime these pages are loaded in browser. When i only want specific page to run the function and not all the pages.


<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager" runat="server">
<Scripts>

        <asp:ScriptReference Path="~/JavascriptFile/MyJavaScriptFile.js"    />
       
     </Scripts>
      
 </ajaxToolkit:ToolkitScriptManager>
0 Votes 0 ·

Hi @peterliles-4155 ,
My meaning is that you could add the script file in your specific page instead of using like you doing.
You could add script in your page like this:

     <script src="~/JavascriptFile/MyJavaScriptFile.js"></script>

Best regards,
Yijing Sun

0 Votes 0 ·

I would like to know a alternative method of loading the function other than when pages are opened in browser?

0 Votes 0 ·

Hi @peterliles-4155 ,
Do you care of using codes to call the jquery file in the code-behind?
Best regards,
Yijing Sun

0 Votes 0 ·
peterliles-4155 avatar image
0 Votes"
peterliles-4155 answered YijingSun-MSFT commented

Although i have another page that utilizes same function so i placed into a global file instead of keeping on page.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @peterliles-4155 ,
I know you have another page that also have the same function,but however, there are no ways to set the special conditions in the global file to make sure the script file only effect on the two pages. I think you only add the script again on the another page instead of the global file.
Best regards,
Yijing Sun

0 Votes 0 ·