Home
 Index > ASP.NET Web Applications > How to add a JavaScript file to a MasterPage

ASP.NET Web Applications:  MasterPage

How to add a JavaScript file to a MasterPage

While the stylesheet href is resolved automatically, script file references are not. The workaround is simple.

Added on 19 Jun 2008

Scenarios:

Scenario Summary:
If you attempt to add runat=server to automatically resolve the url of a script reference in the header, it will be treated as server-side code. If you use RegisterStartupScript(), the reference will be between the BODY tags instead of the HEAD tags.
Scenario Details:
Added on 19 Jun 2008

Solution Summary:
Modify the script source tag to use ResolveClientURL:
Solution Details:

Resolve client URL to get the correct path:

<head runat="server">
    <title>Untitled Page</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <script language="javascript" type="text/javascript" src="<%= Page.ResolveClientURL("~/javascript/global.js")%>"></script>
</
head>
Was this solution useful? Yes No Added on 19 Jun 2008
Rating: 

Copyright 2010 © E-Centric, Inc. | Terms of Use