未捕获的安全错误:阻止了源为“null”的框架访问源为“null”的框架。协议、域和端口必须匹配。

9
我在我的页面上设置了一个iframe,并使用以下脚本来删除双重导航栏。它在任何浏览器上都很好用,但在Chrome上不起作用,它会显示双重垂直导航栏!
function calcHeight() {
    //find the height of the internal page
    var the_height = document.getElementById('the_iframe').contentWindow.
                     document.body.scrollHeight;

    //change the height of the iframe
    document.getElementById('the_iframe').height = (the_height + 30) +"px";
}

我在Chrome中收到2个错误信息:

  • First error message:

    Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

    and it points out the error at:

    var the_height = document.getElementById('the_iframe').contentWindow.
                     document.body.scrollHeight;
    
  • Second error message, it is located in jquery-1.10.2.js:

    Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

    It points out the error message at:

    elem.contentDocument || elem.contentWindow.document : 
    

这是由于同源策略。您应该包括该页面和iframe中的页面的位置。 - Oriol
谢谢你,Oriol!第一个错误来自我的页面,第二个错误来自jquery-1.10.2.js。你能否更详细地指导一下“应该包括该页面和iframe中页面的位置”? - abcid d
我的意思是你应该说出你页面的URL(例如http://example.com/path/file.php)以及加载在`iframe`中的文档的URL(即`<iframe src="<!-- this URL -->"></iframe>`)。这样我们就可以看到它们是否具有相同的来源。 - Oriol
谢谢你,Oriol!我的页面是:JSExample.html,这个页面包含一个iframe:<iframe src="JS1.html" width="110%" height="100%" name="mainIFrame" id="the_iframe" class="iframeHideScr" frameborder="0" onLoad="calcHeight();"></iframe>。这两个页面(JSExample.html和JS1.html)在同一个文件夹和同一级别中。 - abcid d
1
那么看起来应该可以工作。这些页面是本地的吗?如果是这样,请参阅在Chrome中使用本地文件的iframe - Oriol
2个回答

1
尝试在您的机器上设置服务器并从那里测试您的页面,而不是在本地文件系统上进行测试。
如果您有Python 2,请执行python -m SimpleHTTPServer [port]。如果您有Python 3,请执行python -m http.server [port]。这将在localhost:[port]上设置一个服务器。然后启动浏览器并导航到您的页面,看看问题是否解决。

-4

你可以尝试在你的页面中添加document.domain = 'yourdomain.com'


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接