A project I'm working on requires the use of jQuery on customers' Web pages. Customers will insert a chunk of code that we'll supply which includes a few <script>
elements that build a widget in a <script>
-created <iframe>
. If they aren't already using the latest version of jQuery, this will also include (most likely) a <script>
for Google's hosted version of jQuery.
The problem is that some customers may already have an older version of jQuery installed. While this may work if it's at least a fairly recent version, our code does rely on some recently introduced functionality in the jQuery library, so there are bound to be instances when a customer's jQuery version is just too old. We can't require that they upgrade to the latest version of jQuery.
Is there any way to load a newer version of jQuery to use only within the context of our code, that will not interfere with, or affect, any code on the customer's page? Ideally, maybe we could check for the presence of jQuery, detect the version, and if it's too old, then somehow load the most recent version just to use for our code.
I had the idea of loading jQuery in an <iframe>
in the customer's domain that also includes our <script>
, which seems like it might be feasible, but I'm hoping there's a more elegant way to do it (not to mention without the performance and complexity penalties of extra <iframe>
s).
I would like to say that you must always use jQuery latest or recent stable versions. However if you need to do some work with others versions then you can add that version and renamed the
$
to some other name. For instanceLook here if you write something using
$
then you will get the latest version. But if you need to do anything with old then just use$oldjQuery
instead of$
.Here is an example
Demo
Absolutely, yes you can. This link contains details about how you can achieve that: https://api.jquery.com/jquery.noconflict/.
After looking at this and trying it out I found it actually didn't allow more than one instance of jquery to run at a time. After searching around I found that this did just the trick and was a whole lot less code.
So then adding the "j" after the "$" was all I needed to do.
Yes, it's doable due to jQuery's noconflict mode. http://blog.nemikor.com/2009/10/03/using-multiple-versions-of-jquery/
Then, instead of
$('#selector').function();
, you'd dojQuery_1_3_2('#selector').function();
orjQuery_1_1_3('#selector').function();
.It is possible to load second version of the jQuery use it and then restore to the original or keep second version if there was no jQuery loaded before. Here is an example:
You can have as many different jQuery versions on your page as you want.
Use
jQuery.noConflict()
:DEMO | Source