jhanifen
jhanifen

Reputation: 4591

jQuery only allow one version

I have a third party script that is including jquery (which causes things to break). I am using 1.6 version and the external script is using an older version.

How can I force that only one version of jquery is loaded and it is the version I have on the page not the one loaded externally?

Upvotes: 3

Views: 1026

Answers (2)

Roko C. Buljan
Roko C. Buljan

Reputation: 206227

USE .noConflict() instead?
Ex:
I have this into my <head> inside the HTML document:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<script type="text/javascript">
jQuery.noConflict()
jQuery16 = jQuery
</script>

And into my jQ script I call it like:

jQuery16(document).ready(function($){


$('.textarea').text(' Hello World! ');



});

Doing so I'm shure that MY script will not interfer with an external that uses some other / older version of jQuery.

Upvotes: 1

Stephane Gosselin
Stephane Gosselin

Reputation: 9148

This is actually pretty easy to do, using jquery.noConfict();

<script src='jquery-1.6.js'></script>
<script>
var jq16 = jQuery.noConflict();
</script>
<script src='jquery-1.4.2.js'></script>
<script>
var jq142 = jQuery.noConflict();
</script>

You can then have functions use the $() namespace as so:

 (function($) {
$('<button>Use jQuery 1.4.2</button>')
    .click(function() {
        alert('Top: ' + $(this).offset().top + '\n' +
            'jQuery: ' + $.fn.jquery);
    })
    .appendTo('body');
})(jq142);

There is a more detailed example and a few extra tips on this page.

Upvotes: 5

Related Questions