简体   繁体   中英

Two third party JavaScript library naming collision

I have a page that grabs these two files dynamically.

  1. http://cdn.dev.skype.com/uri/skype-analytics.js
  2. http://nexus.ensighten.com/somethingelse.js (tracking tool)

Now Skype has following line:

s.trackAction(_t, this);

Unfortunately the tracking tool JavaScript file also has s as an objects and due to which s.trackAction is not valid for Skype and breaks the whole page.

You could do the following:

  1. Load one script.
  2. Save a reference to s to a variable, let's say first_s .
  3. Load the second script
  4. Save the a reference to s to a variable, let's say second_s .

Then, set s to whichever of these that you want to be the default global value (eg for other people's code).

Then, for your own code or any other code you are using, put it inside this type of block:

(function(s) {
    // any code in here that refers to `s` will see the value of second_s
})(second_s);

or

(function(s) {
    // any code in here that refers to `s` will see the value of first_s
})(first_s);

Another option I can think of is to modify the Skype code to use a global variable named skype instead of s and include that modified version in your page.

If the skype code was written properly, it would have an ability to NOT use a simple globally defined name like s . jQuery and others who use the $ symbol have shown everyone how to do this properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM