Referencing javascript files from a masterpage

Referencing a javascript file on a site via the masterpage turned out to be a bit more involved than I thought it should be.
I built a web template to roll out a branding feature which included uploading a javascript file into the style library folder. I then wanted to reference the javascript file in the masterpage. Relative URL's are not an option due to the fact that once you start delving into sub folders the links to the javascript file will break. And the alternative, putting in a reference to the sites base url, seemed to be impossible according to the lack of results via google (and bing)...
The solution I finally stumbled upon was to use the script manager tag.

In my custom master page I found the script manager tag that was already there and replaced it with the following...

  <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    <Scripts>
      <asp:ScriptReference Path="<%$SPUrl:~SiteCollection/Style Library/scripts/myscript.js%>" />
    </Scripts>        
  </asp:ScriptManager>

Hope this saves you some time...

 

Help my beer fund

RocketTheme Joomla Templates