1

Similar question for reference: How do I run different versions of jQuery on the same page?

I have a situation where I have my own version of jQuery(1.4.2), with all sorts of custom functions defined. This version of jQuery is loaded before a third party script, which loads its own version of jQuery (1.4.3), and when this script is loaded it somehow destroys all the custom functions I had. The third party script uses noconflict after jQuery is loaded. Because of the noconflict code, I assume the problem would be fixed if I could load the third party script before anything else, however my environment is such that I cannot guarantee this, however I can run some custom javascript before and/or after loading the script if I load it dynamically.

What I am wondering is if there is some way I can save/restore/protect my own version of jQuery so that the custom methods will be accessible after the third party script runs?

Community
  • 1
  • 1
Muhd
  • 24,305
  • 22
  • 61
  • 78

2 Answers2

2

If you can run custom code before and after this script is loaded I believe this will do it:

var $myJQ = jQuery.noConflict();

// Load the other script.  It should move itself away from the $ and jQuery variables
// if it properly calls noConflict.

var $ = $myJQ;

I believe gets you to what you're after.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • This is kind of what I was thinking of but wouldn't it need to be something more like: `jQuery = window.jQuery = $myJQ; $ = window.$ = jQuery;` for it to be more generally applicable? – Muhd Mar 02 '11 at 02:48
  • @Muhd - Yes, you can also set the window.jQuery and window.$ variables if they're necessary for your scripts. I don't know that I'd consider it required, but it will depend more on how the plugins / scripts that use it are structured. There's no requirement that window.jQuery be defined for jQuery to function - otherwise `var $myJQ = jQuery.noConflict()` wouldn't achieve anything. – g.d.d.c Mar 02 '11 at 16:03
0

From http://blog.nemikor.com/2009/10/03/using-multiple-versions-of-jquery/

<!-- load jQuery 1.1.3 -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.1.3.js"></script>
<script type="text/javascript" src="jquery.dimensions.min.js"></script>

<!-- revert global jQuery and $ variables and store jQuery in a new variable -->
<script type="text/javascript">
var jQuery_1_1_3 = $.noConflict(true);
</script>

<!-- load jQuery 1.3.2 -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.3.2.js"></script>

<!-- revert global jQuery and $ variables and store jQuery in a new variable -->
<script type="text/javascript">
var jQuery_1_3_2 = $.noConflict(true);
</script>

and then

jQuery_1_1_3('<button>Use jQuery 1.1.3</button>')
    .click(function() {
        alert('Top: ' + jQuery_1_1_3(this).offset().top + '\n' +
            'jQuery: ' + jQuery_1_1_3.fn.jquery);
    })
    .appendTo('body');
macarthy
  • 3,074
  • 2
  • 23
  • 24