【发布时间】:2012-08-21 12:29:38
【问题描述】:
我的主页中嵌套了一个 iframe。 iframe 包含以下脚本:
var adfoxPlaceholderId = 'placeholder';
var adfoxWindow = window;
var adfoxDocument = window.document;
var adfoxPlaceholder = adfoxDocument.getElementById(adfoxPlaceholderId);
try {
while((adfoxPlaceholder == null) && (adfoxWindow != window.top)) {
adfoxWindow = adfoxWindow.parent;
adfoxDocument = adfoxWindow.document;
adfoxPlaceholder = adfoxDocument.getElementById(adfoxPlaceholderId);
}
} catch(ex) {
console.log('catch-block');
}
由于安全策略(iframe 和主页来自不同的 url),脚本在adfoxDocument = adfoxWindow.document; 行中断。
我的问题是为什么这个错误没有被 catch 块捕获,就好像它没有被放入 try-catch 块一样?谢谢你。
【问题讨论】:
-
请通过格式化代码使您的问题可读。
-
您有两行带有
adfoxDocument = adfoxWindow.document的行,其中一行在try/catch 块中。你确定第一个是它正在破坏的那个吗? -
我确信这不会发生在 IE、Chrome、Safari 和 FF 上。您收到此错误的浏览器/版本是什么?
-
尝试将 try catch 放在 while 中,并在 catch 中设置 adfoxPlaceholder = null。我不知道这是否会工作,因为没有工作小提琴。但是通常跨域警告在 javascript 中是可以捕获的,我已经做了几次,不是使用 while 循环,而是使用 setInterval。
-
“adfoxDocument = adfoxWindow.document”这一行在这里只有一次,在 try 块中。 “var adfoxDocument = window.document;”是对的。
标签: javascript try-catch