简体   繁体   English

多个JavaScript冲突

[英]Multiple Javascript conflict

I'm trying to get 2 scripts to run on the same page but they won't play nice with each other. 我正在尝试让2个脚本在同一页面上运行,但它们之间的配合效果不好。 One is called TabTop http://www.isdntek.com/tagbot/tabtop.htm and the other is Clic*Pic http://www.isdntek.com/tagbot/gallery.htm , both by isdntek. 一个是isdntek的,名为TabTop http://www.isdntek.com/tagbot/tabtop.htm ,另一个是Clic * Pic http://www.isdntek.com/tagbot/gallery.htm I can get either one of them to run fine all by themselves, but not both together. 我可以让他们中的任何一个单独运行良好,但不能一起运行。 I looked around and tried to find the answer to this problem by myself, but to no avail. 我环顾四周,试图自己找到解决这个问题的办法,但无济于事。

I would greatly appreciate any help that can be provided. 我将不胜感激,可以提供任何帮助。

Thanks! 谢谢!

You can wrap each script in a self calling function: 您可以将每个脚本包装在一个自调用函数中:

(function(){
  //As long as you don't use global variables
  //the content here is protected from any interaction with the outside
})();

Now, if both codes use global variables, the task will be unfortunately harder. 现在,如果两个代码都使用全局变量,那么不幸的是,任务将更加艰巨。

The RainbowCodeModule6.js file is used by both pages, it sets a very large number of global variables (quite a few because it doesn't declare local variables within functions), so it is quite possible that with two scripts trying to use the same set of globals, they are getting conflicts. 两个页面都使用RainbowCodeModule6.js文件,它设置了大量的全局变量(之所以如此,是因为它没有在函数中声明局部变量),因此很可能两个脚本试图使用相同的全球化的世界,他们正在发生冲突。 eg (my wrapping for posting here) 例如(我在此处包装的包装纸)

function changeShades(color){ //--update the vertical column of light/dark shades
    var ymax=paletteymax
    if (!color){return}
    for (i=0; i<ymax; i++){
    document.getElementById('colorShades'+i).
           style.backgroundColor=colorBrightness(color,(ymax-1-i)/(ymax-1))
    }
}

The above doesn't keep it's counter i local and depends on the global paletteymax . 上述不保留它的计数器i的本地和取决于全球paletteymax I can't say if that's your problem, but it is indicative of poor programming and application architecture. 我不能说这是否是您的问题,但这表明编程和应用程序体系结构不佳。 Another example: 另一个例子:

function dec2hex(R,G,B) { //--Converts three R-G-B components to
                          //  a single internet hex color
    var hexTest="0123456789ABCDEF";
    Rd_hi=R/16; Rd_lo=R%16;
    Rd=hexTest.substr(Rd_hi,1)+hexTest.substr(Rd_lo,1)
    Gn_hi=G/16; Gn_lo=G%16;
    Gn=hexTest.substr(Gn_hi,1)+hexTest.substr(Gn_lo,1)
    Bu_hi=B/16; Bu_lo=B%16;
    Bu=hexTest.substr(Bu_hi,1)+hexTest.substr(Bu_lo,1)
    hexval='#'+Rd+Gn+Bu
    return hexval;
  }

Why they decided to keep hexTest local but let all the other variables go global is beyond me. 他们为什么决定将hexTest保留hexTest本地,但让所有其他变量都hexTest全局,这超出了我的范围。 Variables R , G and B are also global, but here they are kept local because they are formal parameters in the function declaration. 变量RGB也是全局变量,但是这里它们保持局部,因为它们是函数声明中的形式参数。

It also uses document.write to write a table in parts, which is never a good idea. 它还使用document.write来部分编写表格,这从来都不是一个好主意。 I think it's just a poorly written script, find something else. 我认为这只是写得不好的剧本,还有其他东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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