If you enable jQuery version 2 on a page, how do you limit the plugin to only one of them?

So, this question is not as crazy as it might seem at first glance. I am developing a piece of javascript code to access my client pages. I am worried that they are using a different version of jQuery on their site.

I know from Jquery docs something like this should work.

var dom = {};
dom.query = jQuery.noConflict(true);

The problem is that I also use some jquery plugins, namely fancybox and mousewheel . So, how can I make sure that my version of jQuery is the only one that can use these plugins. I don’t want a conflict to occur if one of our customers used the same versions on their pages, but different versions.

The only specific way I can think of now is to change the plugins so that they only call dom.query

+3
source share
2 answers

There is a trick you could use. If your jQuery version is the second one that is loading, you need to go to the security of your version first before downloading it, something like this

<script type="text/javascript">
    (function($){
       var theirs_jquery = $;
    })(jQuery);
</script>

Then add the jQuery version of the script include version

<script src="link/to/my/jquery.js"></script>
 <!-- add your plugins -->
<script src="link/to/my/jquery/plugins/plugin1.js"></script>
<script src="link/to/my/jquery/plugins/plugin2.js"></script>

then add another script block followed by

<script type="text/javascript">
(function(){
    var $$ = jQuery; // this is your shortcut
    var $ = theirs_jquery; // put back their version
})(jQuery);
</script>

after that you can access your version of jquery with $$ shortcut

+3
source

you can use

(function($) {        

})(dom);

To wrap up calls.

This maps dom to $ for code only, replacing any external $ definition.

, jQuery, , jQuery jQuery.

0

Source: https://habr.com/ru/post/1793712/


All Articles