简体   繁体   中英

Two jQuery versions on the same page

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

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.

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM