Include a specific version of JQuery and a plugin without conflicting with the page's JavaScript library? - Stack Overfl

TL;DR: I want to include a specific version of JQuery and a specific JQuery plugin that is associated w

TL;DR: I want to include a specific version of JQuery and a specific JQuery plugin that is associated with my inclusion of jQuery on multiple different sites where I know nothing about the JavaScript libraries/plugins used by them, without conflicts. Possible? How?

The whole story: Let's say I want to build a Reddit platform where multiple sites can link to Reddit threads by including .js and having <a href="" data-reddit-thread-id="1234"></a> elements littered on the page. Once a user clicks on a link like that, I open reddit in a beautiful modal window.

Now because I want Reddit Platform experience to be consistent across all sites that use my script, I want all sites to use my particular modal plugin and I want to style the links in my particular fashion.

But I can never be sure what Javascript library or plugins the other sites are using, so I must make sure that when reddit.js loads JQuery, it must not conflict with other libraries on the page and when reddit.js loads, for example, prettyPhoto plugin it should only be associated with the jQuery library included by me.

But I also want widespread adoption for my platform, so I don't want to place any more restrictions on the sites like you must have jQuery, or you must have prettyPhoto loaded. I just want them to include this one JS file, reddit.js which does everything for them.

Is it possible to make this work? How would you go about it?

Thanks!

TL;DR: I want to include a specific version of JQuery and a specific JQuery plugin that is associated with my inclusion of jQuery on multiple different sites where I know nothing about the JavaScript libraries/plugins used by them, without conflicts. Possible? How?

The whole story: Let's say I want to build a Reddit platform where multiple sites can link to Reddit threads by including http://myredditplatform./reddit.js and having <a href="" data-reddit-thread-id="1234"></a> elements littered on the page. Once a user clicks on a link like that, I open reddit in a beautiful modal window.

Now because I want Reddit Platform experience to be consistent across all sites that use my script, I want all sites to use my particular modal plugin and I want to style the links in my particular fashion.

But I can never be sure what Javascript library or plugins the other sites are using, so I must make sure that when reddit.js loads JQuery, it must not conflict with other libraries on the page and when reddit.js loads, for example, prettyPhoto plugin it should only be associated with the jQuery library included by me.

But I also want widespread adoption for my platform, so I don't want to place any more restrictions on the sites like you must have jQuery, or you must have prettyPhoto loaded. I just want them to include this one JS file, reddit.js which does everything for them.

Is it possible to make this work? How would you go about it?

Thanks!

Share Improve this question asked Jul 15, 2012 at 11:02 Gaurav DadhaniaGaurav Dadhania 5,3378 gold badges45 silver badges63 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Below is a stripped down version of my widget loader which does basically what you want.

The key line of code is jquery1_8_2 = jQuery.noConflict(true);

More info here: http://api.jquery./jQuery.noConflict/

var myWidgetLoader = function() {

   this.start = function() {
      this.getScript('https://ajax.googleapis./ajax/libs/jquery/1.4.2/jquery.min.js', function(){

         jquery1_8_2 = jQuery.noConflict(true);  // true = unconflict all jQuery vars, including "jQuery"
         (function(jQuery) {
            var $ = jQuery;
            // do stuff that uses jQuery
         })(jquery1_8_2);

      });
   }

   this.getScript = function(url, success) {
      var script = document.createElement('script');
      script.src = url;
      var head = document.getElementsByTagName('head')[0], done=false;
      script.onload = script.onreadystatechange = function(){
         if ( !done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'plete') ) {
            done = true;
            success();
         }
      };
      head.appendChild(script);
   }

}

myWidgetLoader.start();

You could make a custom build of jQuery that doesn't clobber the globals. Have a look at the source: https://github./jquery/jquery/blob/master/src/exports.js. You could modify this line:

window.jQuery = window.$ = jQuery;

to be like this:

window.myRedditPluginJquery = jQuery;

Then when you run your code, you need to use myRedditPluginJquery as $. Something like this IIFE:

(function($){
    //your code goes here
    //you also probably have to paste the source code for that plugin here too
})(myRedditPluginJquery);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742414685a4439511.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信