Microsoft MVP Logo

I saw another blog post today where someone was showing how to use a delegate control to add the jQuery library to every page on a site. While that does work, there is a newer and much more elegant solution that doesn't require nearly the same amount of work: the ScriptSrc attribute introduced in SharePoint 2010.

All you have to do is create a new custom action in a Feature and add a relative reference to the jQuery, or any JavaScript library for that matter. The library reference will be added to every page for the scope of the Feature so using this technique you could even add it to every single page in the farm. Here's what it looks like...

<CustomAction Location="ScriptLink" ScriptSrc="~/layouts/jQuery/jquery-1.5.2.min.js" />

I've only found two downsides to this approach. The first is that the jQuery library isn't being pulled from a CDN like Google or Microsoft's hosted options. I prefer to pull from these as it's likely the user has already pulled from them before and it's cached on their machine. The other downside is that I've seen this approach break the Asset Picker in SharePoint 2010 Publishing [WCM] sites.

One parting thought: I don't think it's a good idea to globally add a script to every page UNLESS you need it on every page. Else you're simply adding page weight for no good reason.

Comments powered by Disqus