开发者

Bundle jQuery with widget

Let's say I want to provide a widget like You开发者_运维百科tube 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜