Marie Marie - 1 month ago 4x
jQuery Question

Is it possible to copy all plugins from one version of the jQuery object to another?

I am working on a page that relies on a lot of jQuery 1.9.1 features while the site itself relies on jQuery 1.7.1. 1.7.1 is loaded first and all plugins execute against it. On certain pages after any plugin scripts 1.9.1 is added and assigned to a variable with

. The problem is that I need access to jQuery UI and a few other miscellaneous plugins from 1.9.1 objects.

Is there some common plugin parameter or something I can just reference from the second version? Something like
jQuery191.plugins = $.plugins
maybe? Or do I have to reload the jQuery plugins file before my noConflict?


I'm sure it goes without saying, but ideally update the site to use a single, up-to-date version of jQuery, which makes this problem go away.

The answer to your question, though, is no, you can't copy them — at least, not with properly-written plugins. The high-level structure of a properly-written plugin is along these lines:

(function($) {
    // Do things with $ here, such as registering a plugin function:
    $.fn.thePlugin = function() {
        // ...and presumably here as well...

That means the plugin has a reference to the version of jQuery that controlled the jQuery symbol when the plugin was loaded.

Later, after loading a different version of jQuery, you might be tempted to do this:

jQuery.fn.thePlugin = oldJQuery.fn.thePlugin;

...but then you'd have horrible cross-talk between jQuery versions: The plugin would be called by the newer jQuery, but use the older jQuery when it did its thing.

Now, it happens that with my greenPlugin above

So until you can clean things up, you'll have to duplicate your loading of the plugins:

<script src="jquery-1.7.1.js"></script>
<script src="this-plugin.js"></script>
<script src="that-plugin.js"></script>
<script src="the-other-plugin.js"></script>
var jq171 = jQuery.noConflict(); // With or without `true`
<script src="jquery-1.9.1.js"></script>
<script src="this-plugin.js"></script>
<script src="that-plugin.js"></script>
<script src="the-other-plugin.js"></script>

...which is obviously ugly. :-) But the scripts will presumably come from cache the second time, at least. And it means that the version of jQuery accessible via jq171 and the one accessible via jQuery/$ both have all three plugins.