Reputation: 8993
Let's say I want to provide a widget like Youtube does to embed their player in an external page. For this widget, I rely on a certain version of jQuery. How can I bundle jQuery with my widget so that it is available to my widget JS code, but that it doesn't interfer with other jQuery versions included in the external page?
Do you have any pointers how to do that? Basically, I'm looking for a way to load jQuery into a specific namespace.
Upvotes: 1
Views: 352
Reputation: 1333
You can do that with the following:
var $j = jQuery.noConflict(true);
which will remap all variables from jQuery/$ to $j
Your code would have to use $j in your code instead, unless you wrap it in a closure:
(function($){ /* jquery code here */ })($j);
Normally I would just pass jQuery into that self-executing function, but since you're worried that the parent site might have an older version of jQuery loaded, it'd be safer to combine both the self-executing function with noConflict
Upvotes: 3