Reputation: 4445
I have some jQuery events on a Master page, which needs to run on all of the content pages. If the content page is in the same directory as the master page, it works fine. However, if the content page is in a subdirectory, the page loads fine, but the jQuery doesn't run.
The jQuery is not much and is just between <script></script>
tags. If possible, I'd like to keep it this way as opposed to using an ASP ScriptManager
.
Upvotes: 0
Views: 1546
Reputation: 32738
It's likely that your script itself is fine, but your jQuery references are messed up due to incorrect relative paths. You should be able to verify by using your browser's debugging tools, you'll spot where it failed to load jQuery.
<script type="text/javascript" src='<%=ResolveUrl("~/path/jquery.js") %>'></script>
Using the above, we get the correct site relative path from the server side. Then the reference to jQuery should be correct no matter what folder you're in.
When master pages are combined with content pages, the paths to the scripts are not updated if you hardcode the path to the script. So if you're in the same folder as your master page, the path to your scripts are correct. But if you're in a different level of the folder structure, the path becomes incorrect. That's why we use ResolveUrl() from the server side, it's able to give us the correct path on the client side regardless of the folder we're in.
Upvotes: 1