Referencing JavaScript files from MasterPage
To avoid problems with pathes you can you “~” at the start of your ulr. But this works only for controls with runat=”server” attribute. You can add this attribute to head tag.
<head runat="server">
It works fine for css links
<link href="~/styles/style.css" rel="stylesheet" type="text/css" />
But if you will try to add this attribte to scritp tag you will have compiler error
<script type=”text/javascript” src=”~/scripts/myScript.js” runat=server/>
But there are another solution, use ResolveUrl method
<script src="<%=ResolveUrl("~/Scripts/jquery-1.3.2.js") %>"
type="text/javascript"></script>
But somtimes this method trow error
The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>).
It’s because of “=” in “<%=”. But it can be resolved very easy using “#” databinding expression.
<script src="<%#ResolveUrl("~/Scripts/jquery-1.3.2.js") %>"
type="text/javascript"></script>
But do not forget to bind this url in pageLoad event
protected override void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
Page.Header.DataBind();
}
Trackback from your site.