11

NOTE: I know similar questions have already been asked here and here, but I'm looking for additional clarification as to how to make this work or good reasons for avoiding it entirely.

I'm adding functionality to an existing web site that is already using an older version of the jQuery library (1.1.3.1). I've been writing my added functionality against the newest version of the jQuery library (1.4.2). I've tested the website using only the newer version of jQuery and it breaks functionality, so now I'm looking at using both versions on the same page. How is this possible?

What do I need to do in my code to specify that I'm using one version of jQuery instead of another? For example, I'll put <script> tags for both versions of jQuery in the header of my page, but what do I need to do so that I know for sure in my calling code that I'm calling one version of the library or another?

Maybe something like this:

//Put some code here to specify a variable that will be using the newer
//version of jquery:
var $NEW = jQuery.theNewestVersion();

//Now when I use $NEW, I'll know it's the newest version and won't
//conflict with the older version.
$NEW('#personName').text('Ben');

//And when I use the original $ in code, or simply 'jquery', I'll know
//it's the older version.
$('#personName').doSomethingWithTheOlderVersion();

UPDATE: After reading some of the answers, I'm starting to wonder if it's even a good idea to try to play this game at all...

Community
  • 1
  • 1
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223

5 Answers5

24

You can use the jQuery noConflict method to achieve this.

<script type="text/javascript" src="jquery-1.1.3.js"></script>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
    //Here, $ refers to the most recent jquery script loaded, which is version 1.4.2.
    $.noConflict();

    //Here, jQuery refers to 1.4.2 and $ refers to 1.1.3
    //You can setup an alias like so:
    var $NEW = jQuery;

    //Now, you can use old jQuery with $ and new jQuery with $NEW.
</script>
EndangeredMassa
  • 17,208
  • 8
  • 55
  • 79
  • Thanks for the code. Is it safe to assume that when I call `$.noConflict()` that I'm always calling the version of jQuery that's listed second in my ` – Ben McCormack May 14 '10 at 21:24
  • @Ben Yeah. The second script is loaded and saves a reference to the first $ variable in case you call noConflict. – EndangeredMassa May 14 '10 at 23:41
7

Possible? Yes, in the same way that it's possible to run jQuery and another framework that uses the name $ at the same time. Include one copy of jQuery and assign it to a new name using noConflict, then do the same to another copy.

A good idea? Really no, in the same way that running jQuery and another framework at the same time isn't a good idea, only more so. jQuery is a far-reaching, invasive framework. If two instances of jQuery start mutating the same element (and versions of jQuery before 1.4 are very promiscuous about what elements they touch), they are likely to confuse each other with unpredictable, timing-sensitive and undebuggable side-effects.

If at all possible, updating all the code to run under the latest jQuery is by far the better route. This really shouldn't be that difficult; the jQuery devs haven't broken much that code should ever have relied on in the first place.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • I agree with updating the existing code being a far better idea. However, at least as far as I know, jQuery is way less invasive than many of the other libraries (I haven't seen "noConflict" in other libraries, for instance) – Matti Virkkunen May 14 '10 at 20:49
  • jQuery adds an expando property to all elements it ‘touches’, which is all elements that have any `data` attached (such as event handlers), and prior to 1.4 all elements that it ever even checks for having data attached (which can easily be most of the document — incredibly invasive!). If another framework or version copies, deletes or changes those properties, jQuery will be left in an inconsistent state. – bobince May 15 '10 at 10:13
  • I agree with your premise that it's probably not a good idea. I'm stuck, though, because upgrading the site to use 1.4.2 simply isn't an option. Oh the joys of dynamic languages... – Ben McCormack May 15 '10 at 15:23
2

You should not do this what so ever, $.noConflict() won't really help you in all cases.
This is because, internally, jQuery uses the alias name jQuery so forget about
any dollar sign $ re-assinging... if you will use any code that's using jQuery like this:
jQuery('div').doSomethingDepricated() then it will use the last defined jQuery loaded in the page. Now, if you use a plugin, which only works with old jQuery, and internally
it's written only using jQuery alias (not $), then it will break.

If you really want to use 2 jQuery scripts with different versions, you can do
search and replace on the 'jQuery' name on one of them, and change it to something else, then you can also re-assign it do any alias name via no-conflict method if you wish.

vsync
  • 118,978
  • 58
  • 307
  • 400
1

As an extension of EndangeredMassa's solution, I propose the following modification:

<script type="text/javascript" src="jquery-1.1.3.js"></script>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
    //Here, $ refers to the most recent jquery script loaded, which is version 1.4.2.
    $.noConflict();

    //Here, jQuery refers to 1.4.2 and $ refers to 1.1.3
    //You can setup an alias like so:
    var $NEW = jQuery;

    // jQuery needs to refer to 1.1.3 again and not to 1.4.2
    var jQuery = $;

    //Now, you can use old jQuery with $ and new jQuery with $NEW.
</script>
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Nico Napoli
  • 1,867
  • 2
  • 13
  • 12
0

Fix your old code to work with a recent jQuery version. Everything else is extremely hackish and will call the wrath of all developers who might have to work with your code in the future on you.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • I wish I could "fix" the old code, but it was written by a 3rd party who was contracted to update our web site as long as it's not completely broken, the company isn't going to invest the time to upgrade the site. Thems the breaks in business sometimes. – Ben McCormack May 15 '10 at 15:15