12

Is it possible to have 2 different jQuery versions in the same document, and have them to not collide with each other?

For example if I create a bookmarklet and want to base the code on jQuery. This bookmarklet is injected on some page that uses another version of jQuery then my code would overwrite the version used on the page.

Is it possible to avoid that? Or are there some other libraries that provides this functionality Or maybe I should rethink the whole thing.

Thanks for answers and pointers,

bob

  • 1
    You could check if jQuery is already on a page before injecting it again. – Joe Chung Jul 13 '09 at 01:58
  • 3
    You should definitely rethink it. You should update older functions to work with the new jQuery, including both is just a ton of bloat and a wish for collisions. – Ian Elliott Jul 13 '09 at 01:59
  • Have you got control over the version of jQuery being added to the page? You don't want your visitors to have to download ANOTHER version of the library if they can help it - it's bad practice and inflates the filesize of the document for your visitors. – Jason Berry Jul 13 '09 at 04:58
  • possible duplicate of [Can I use multiple versions of jQuery on the same page?](http://stackoverflow.com/questions/1566595/can-i-use-multiple-versions-of-jquery-on-the-same-page) – a'r Sep 23 '11 at 11:28

3 Answers3

12

jQuery comes with a way to avoid collisions. After you load the first version, you can assign it to a different variable.

var $j = jQuery.noConflict();

And then load your second jQuery version. The first one you load can be accessed with $j(...) while the second one can be accessed with $(...).

Alternatively, somebody made a little helper in an attempt to make it easier to switch between different versions.

Chris
  • 257
  • 1
  • 9
  • bob doesn't control when the first version is loaded as this is happening on a 3rd party site via a bookmarket. you could call noConflct before adding the newer version via createElement('script') - which would 'upgrade' the 3rd party site. can you assume the 3rd parties code with be compatible? – russau Jul 14 '09 at 22:06
  • With the Versions helper, you can switch back and forth between different versions of jQuery whenever you want. Just say Versions.use('jquery', 'default') or Versions.use('jquery', 'latest') after you have added them to switch. If his code is using the newest version and the third part site isn't, he just has to make sure that he sets the version correctly at the beginning and end of his code. – Chris Jul 16 '09 at 14:45
2

Here is a way to detect is JQuery is already present: jQuery in widget

Work out the oldest version of JQuery your code will work with.. And refuse to work if the version present is too old. Only a few people will miss out, most sites using JQuery are pretty up to date..

Community
  • 1
  • 1
russau
  • 8,928
  • 6
  • 39
  • 49
0

Why not just have different versions of your javascript, for different versions of jquery, so, look at what version is on the page and get the appropriate code that will work on that version of jquery.

This would be safer, as anything else may be very fragile, as it sounds like you don't have control over the version of jquery that will be on the page.

James Black
  • 41,583
  • 10
  • 86
  • 166