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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25893006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:01:49  来源:igfitidea点击:

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

javascriptiframesame-origin-policy

提问by abcid d

I set an iframe on my page and use below script to remove the double navigation bars. It works well on any browser, but doesn't work on Chrome, it shows double vertical navigation bars!

我在我的页面上设置了一个 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";
}

I get 2 errors messages in Chrome:

我在 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 : 
    
  • 第一条错误信息:

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

    它指出了以下错误:

    var the_height = document.getElementById('the_iframe').contentWindow.
                     document.body.scrollHeight;
    
  • 第二条错误信息,它位于 jquery-1.10.2.js 中:

    未捕获的安全错误:无法从“HTMLIFrameElement”读取“contentDocument”属性:阻止原点为“null”的框架访问原点为“null”的框架。协议、域和端口必须匹配。

    它指出错误消息:

    elem.contentDocument || elem.contentWindow.document : 
    

回答by Yibo Yang

Try setting up a server on your machine and test your page from there, instead of on your local file system.

尝试在您的机器上设置一个服务器并从那里测试您的页面,而不是在您的本地文件系统上。

If you have Python 2, do python -m SimpleHTTPServer [port]
In Python 3, do python -m http.server [port]
This will set up a server on localhost:[port]. Then fire up a browser and navigate to your page and see if the problem goes away.

如果你有 Python 2, dopython -m SimpleHTTPServer [port]
在 Python 3, dopython -m http.server [port]
这将在localhost:[port]. 然后启动浏览器并导航到您的页面,看看问题是否消失。

回答by user3559718

You can try to add document.domain = 'yourdomain.com'to you page

您可以尝试添加document.domain = 'yourdomain.com'到您的页面