Search code examples
asp.netconnectionmaster-pages

Do I need to duplicate the script connection in child pages?


In the master page, my script is connected in this way:

<script src="scripts/jquery-3.4.1.min.js" type="text/javascript"></script>

And on the pages at the root, the browser developer console does not show errors, but the following error appears on the child pages:

... /Pages/Scripts/jquery... (Not Found)

Solution

  • No you don't need to duplicate the script tags, it is being rendered on all your pages which use the master page the script just can't be found on pages outside your root. With your current code when a page is rendered it is starting from its current path and appending the src path to determine where to find the script. So your pages in root work but pages in subfolders will not. To correct this resolve your urls properly:

    Webforms:

    <script src="<%=ResolveUrl("~/scripts/jquery-3.4.1.min.js")%>" type="text/javascript"></script>
    

    Razor:

    <script src="@Href("~/scripts/jquery-3.4.1.min.js")" type="text/javascript"></script>