0

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.

Adrien Hingert
  • 1,416
  • 5
  • 26
  • 51

2 Answers2

1

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";
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • Just to be clear: Assuming my library is the first, I execute var j = jQuery.noConflict(); after the second library is loaded and then my library is reference via "j" rather than the usual "$", correct? – Adrien Hingert Jun 21 '14 at 09:37
  • 1
    If you load your own library first, then load jQuery and then execute `j = $.noConflict();` then `j` refers to the last loaded library (in this case, jQuery). – hjpotter92 Jun 21 '14 at 09:42
  • Could I load my library, execute j = $.noConflict(); and then the second library and so "j" would refer to the first library? – Adrien Hingert Jun 21 '14 at 09:44
  • 1
    @AdrienHingert `noConflict` is a jQuery feature. You can't use it with all the other libraries as well. – hjpotter92 Jun 21 '14 at 09:45
0

Websites like Codepen do this by putting the user's code (including the user's jQuery) within an iframe from a different domain origin.

Chirag Bhatia - chirag64
  • 4,430
  • 3
  • 26
  • 35