Loading jQuery multiple times in the same page
I'm implementing a plug-in that's embeddable in different sites (a la Meebo, Wibiya), and I want to use jQuery. Problem is, the site I'm embedding to, may already have a jQuery script loaded.
The question is, what's the best approach for this kind of problem:
Should I just check if jQuery is already loaded and if so, use the original site's jQuery, otherwise load it myself? If I use this approach, am I not risking comaptibility problems (i.e. the site uses an old version of jQuery)?
Should I load jQuery myself (whether it's already loaded or not) and call "jQuery.noConflict(true)" when it's finis开发者_如何学JAVAhed loading? If so, how can I make sure that my jQuery has finished loading (hooking to the onLoad event doesn't seem to work all the time, and polling for "jQuery" won't work for obvious reasons)?
Should I do something else?
Thanks.
You can check if jQuery is already loaded using the code:
if(jQuery) {
//it's loaded
} else {
//it's not loaded
}
You can also check the version of the loaded jQuery using:
$().jquery;
this will return a string like "1.4.1"
Using these two methods you can see what the site has already loaded and act accordingly.
By this blog article
if (typeof jQuery == 'undefined') {
// jQuery is not loaded
} else {
// jQuery is loaded
}
精彩评论