adventures in technology
SharePoint 2010 jQuery compatibility errors – cmssitemanager.js
Recently I used a site collection scoped feature to add jQuery support to a SharePoint 2010 site collection. As documented on countless blogs, I used custom action XML similar to the following:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
ScriptSrc="JqueryCustomAction/jquery-1.4.2.min.js"
Location="ScriptLink"
Sequence="100">
</CustomAction>
</Elements>
Everything seemed to work as intended. Only later did I notice strange JavaScript errors in the SharePoint interface. The most common errors were in a file called cmssitemanager.js when using the Asset Picker dialog as part of a publishing site:
It turns out that SharePoint 2010 has JavaScript that registers a global function named “$”. This conflicts with the same global function which is registered by jQuery and is used as shorthand for the various jQuery functions.
My very quick solution was to reference a second JavaScript file in my custom action. This JavaScript file disables the “$” shorthand function for jQuery. This fixes all the compatibility issues with the only downside being that you now have to replace “$” with “jQuery” when referring to jQuery globally.
Updated script references:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
ScriptSrc="JqueryCustomAction/jquery-1.4.2.min.js"
Location="ScriptLink"
Sequence="100">
</CustomAction>
<CustomAction
ScriptSrc="JqueryCustomAction/jquery-noconflict.js"
Location="ScriptLink"
Sequence="105">
</CustomAction>
</Elements>
No conflict JavaScript:
// this puts jquery into no conflict mode which will remedy conflicts caused by jQuery when used // with certan publishing features jQuery.noConflict();
Below is a minimal Visual Studio 2010 solution that contains a site collection scoped jQuery feature.
| Print article | This entry was posted by brad on January 26, 2011 at 2:17 pm, and is filed under Software. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
Pingback: Tweets that mention SharePoint 2010 jQuery compatibility errors – cmssitemanager.js -- Topsy.com
Pingback: $ in cmssitemanager.js conflicts with $ in jQuery « Share… What?