2

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:

  1. 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)?

  2. Should I load jQuery myself (whether it's already loaded or not) and call "jQuery.noConflict(true)" when it's finished 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)?

  3. Should I do something else?

Thanks.

2
  • 1
    in case of a plugin that need to run anywhere i would suggest you use pure JS and no frameworks... Butcheck for jQuery, incl. the version and include it, if its not here. (Or the wrong version) Commented Jan 17, 2011 at 9:50
  • Agree with Meo - you should streamline the JS as much as possible, and that would mean not using a third party plugin (jQuery). Keep the files as small as possible. Commented Jan 17, 2011 at 10:07

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

By this blog article

if (typeof jQuery == 'undefined') {  
    // jQuery is not loaded  
} else {
    // jQuery is loaded
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.