解决JavaScript冲突

解决JavaScript冲突,javascript,Javascript,在一个网站上,我有两个Java脚本,加载在带有图像库的iframe中,一个是lytebox,它是用于图像库弹出窗口的Light Box 2精简克隆,另一个是在一堆div中只显示一个div。lytebox脚本一直工作到调用show only一个,从那时起,它在IE FF和Opera中被破坏(Chrome和Safari不会抛出错误),并出现以下错误: Uncaught exception: TypeError: Cannot convert 'window.parent.frames[window.

在一个网站上,我有两个Java脚本,加载在带有图像库的iframe中,一个是lytebox,它是用于图像库弹出窗口的Light Box 2精简克隆,另一个是在一堆div中只显示一个div。lytebox脚本一直工作到调用show only一个,从那时起,它在IE FF和Opera中被破坏(Chrome和Safari不会抛出错误),并出现以下错误:

Uncaught exception: TypeError: Cannot convert 'window.parent.frames[window.name]' to object Error thrown at line 223, column 1 in (imageLink, doSlide, doFrame) in [url to script]: var anchors = (this.isFrame) ? window.parent.frames[window.name].document.getElementsByTagName('a') : document.getElementsByTagName('a'); called from line 204, column 56 in () in [url to script]: myLytebox.start(this, false, false); 未捕获异常:TypeError:无法将“window.parent.frames[window.name]”转换为[url到脚本]中(imageLink、doSlide、doFrame)第223行第1列抛出的对象错误: var锚=(this.isFrame)?window.parent.frames[window.name].document.getElementsByTagName('a'):document.getElementsByTagName('a'); 从[url to script]中()的第204行第56列调用: myLytebox.start(this,false,false); 在它中断后,您必须重新加载页面才能使任何内容重新工作

我假设这是一个冲突,因为它只发生在调用另一个脚本之后。在放映之前,只有一个被调用,它可以完美地工作

这是嵌入页面的仅显示一个代码:

function showonlyone(thechosenone) {  
  var newboxes = document.getElementsByTagName('div');  
        for(var x=0; x<newboxes.length; x++) {  
              name = newboxes[x].getAttribute('name');  
              if (name == 'newboxes') {  
                    if (newboxes[x].id == thechosenone) {  
                    newboxes[x].style.display = 'block';  
              }
              else {  
                    newboxes[x].style.display = 'none';  
              }
        }
 }
}
函数只显示一个(chosenone){
var newbox=document.getElementsByTagName('div');

对于(var x=0;x查找lytebox.js的第223行,并进行以下更改:

var archors=(this.isFrame)?window.parent.frames[window.name].document.getElementsByTagName('a'):document.getElementsByTagName('a')

为此:

var archors=(this.isFrame&&window.parent.frames[window.name].document)?window.parent.frames[window.name].document.getElementsByTagName('a'):document.getElementsByTagName('a')


我的
lytebox.js
出现了两次


建议:查找
(this.isFrame)?
并替换为
(this.isFrame&&window.parent.frames[window.name].document)?

在FF中通过Firebug抛出的确切错误是“window.parent.frames[window.name]未定义”,不确定是否缩小了范围。现在我已将其从“window.parent.frames”更改为“window.parent.frames”[window.name]“to”window.parent.frames['imgthis']”似乎可以处理所有问题。不过我还是想知道为什么第一个不起作用。