Avoiding conflicts when using jQuery and EPiServer Composer
Posted on October 25, 2010 by Frederik Vig in EPiServerOn a customers site today I had some JavaScript errors when using Composer on page edit. After firing up Firebug to inspect the errors I saw that Composer is using Prototype, which is another popular JavaScript library, that also uses the ‘$’ for its wrapper function.
To avoid this conflict I used a popular design pattern, that passes a self executing anonymous function to the jQuery namespace with the jQuery object.
(function($){ // Your jQuery code is safe here })(jQuery) |
This fixed the errors. Definitely goning to add this to all my jQuery code from now on :).
I recommend checking out How Good C# Habits can Encourage Bad JavaScript Habits: Part 1, the whole series is fantastic and teached me quite a few things that I’ll take with me.
Björn Olsson says:
Post Author October 26, 2010 at 11:32I prefer using jQuery.noConflict() myself, it also allows you to use multiple versions of jQuery on the same page.
Elijah Manor says:
Post Author October 27, 2010 at 05:56Glad you enjoyed the C#/JavaScript series on Enterprise jQuery 😉 Thanks!