fooberry

Sweetness without context.

Rebasing Javascript via a MasterPage

January 15th 2009

I’ve been looking for a way to rebase the URL for the javascript we include in our master pages. We add a lot of javascript in the master pages such as the following.

    <link href="../css/ui-theme.css" rel="stylesheet" type="text/css" />
    <script language="javascript" type="text/javascript" src="../js/jquery-1.3.js" />

The problem is, as everyone probably knows, the relative path to the file is from the master page, not from the client page. For some reason, ASP.Net is kind enough to rebase the link tag’s href property, but not the javascript link.

After asking the Google, I saw a lot of examples to solve the problem like by using the ResolveClientUrl as follows:

    <script language="javascript" type="text/javascript" 
        src='<%= ResolveClientUrl("~/js/jquery-1.3.js") %>' />
    <!-- or -->
    <script language="javascript" type="text/javascript" 
        src='<%# ResolveClientUrl("~/js/jquery-1.3.js") %>' />

However, I saw no one suggest this, which I believe to be a little cleaner.

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        <Scripts>
            <asp:ScriptReference Path="~/js/jquery-1.3.js" />
        </Scripts>
    </asp:ScriptManager>

Granted, this brings in some ASP.Net Ajax that may ugly things up, but it works for our own javascript files.

blog comments powered by Disqus