When you’re developing web parts and on demand solutions, in other words: you’re working on an individual component rather than a whole portal, you’re bound to run into this issue.
Scenario: you’re creating a web part for a client who runs existing SharePoint 2010 or 2007, or even 2013; also, let’s assume your web part uses jQuery to do something fancy – it’s hard not to use jQuery these days. If you client’s portal already calls jQuery in a master page and you’re registering jQuery in your web part again – those two will collide and you’re going to break the entire site.
One solution is, of course, to ask your client if they use jQuery, but that’s not always possible; especially if you’re working on a packaged solution you’re planning to distribute online. Also, even if they don’t use jQuery in a master page, how do you know that there is no 3rd party web part on the same page which calls jQuery.
So, the most reliable way to resolve this issue, is to verify if the jQuery has been registered and only register it when it’s not.
Let’s say in your visual web part ASCX control, where you’d normally register jQuery, call the following script instead:
window.jQuery || document.write('<script src="/_layouts/MySolution/Scripts/jquery-1.9.1.min.js">\x3C/script>');
Above, we’re checking if jQuery is registered and if not, we output a string which registers it. In our case we’re sourcing the jQuery file from the local _layouts folder, but you can source it from online or a document library etc.
That’s it … here is the output of such call in case jQuery has not been registered:
That’s it, now you’re safe against breaking your client’s site!
More tricks like this in my new SharePoint 2013 dev book.