Search code examples
jquerysizzle

Multiple versions of jQuery


I am working on service which allows third parties to upload HTML snippets. In some of these snippets there may be links to jQuery, SizzleJS... libraries and JavaScript code that requires specific versions of these libraries. The service obviously requires it's own version of jQuery.

Is there any way I could load multiple versions of jQuery (or just SizzleJS) and have my own code only referencing my version of jQuery while third parties reference any version they may be linking to? Maybe using different namespace? How would I do this?

I hope my question is clear enough.


Solution

  • You have a noConflict() call:

    If for some reason two versions of jQuery are loaded (which is not recommended), calling $.noConflict( true ) from the second version will return the globally scoped jQuery variables to those of the first version.

    One of the best implementation can be shown in following snippet:

    var j = jQuery.noConflict();
    // Do something with jQuery
    j( "div p" ).hide();
    // Do something with another library's $()
    // This other can library be an older (or newer) jQuery as well
    $( "content" ).style.display = "none";