SharePoint: referencing SCRIPT done right

Our scenario is pretty simple – we have a package that includes some JavaScript files.  We had them deployed to /Style Library/ similar to our CSS resources.

We then refer to them in our page layouts and master page via

<script src=”/Style Library/project/myscript.js” />

It works fine on dev.


This makes no assumption about the web application or site collections of the destination server.  When the site was exported and imported into various different site collection configurations, things start to go silly with 404 script file not found.

Site collection:
The JavaScript then is at: Library/project/myscript.js
But the reference is still looking for Library/project/myscript.js
Oops – 404 error.


The solution comes in a two steps:

  1. Deploy to _layouts/ instead, so the script resource is at
  2. Use the SharePoint control ScriptLink
    <SharePointWebControls:ScriptLink Name=”/project/myscript.js” Localizable=”false” runat=”Server” />
  3. The localizable = false is important – otherwise SharePoint will look for