未捕获的SecurityError:阻止了一个具有原点的帧“;空”;从访问具有原点的帧“开始”;空”;.协议、域和端口必

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

本文关键字:原点 协议 开始 访问 一个 SecurityError      更新时间:2023-09-26

我在页面上设置了一个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条错误消息:

  • 第一条错误消息:

    未捕获的SecurityError:阻止原点为"null"的帧访问原点为"null"的帧。协议、域和端口必须匹配。

    并指出错误:

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

    未捕获的SecurityError:未能从"HTMLIFrameElement"读取"contentDocument"属性:阻止原点为"null"的帧访问原点为"null"的帧。协议、域和端口必须匹配。

    它在以下位置指出错误消息:

    elem.contentDocument || elem.contentWindow.document : 
    

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

如果您有Python 2,请执行python -m SimpleHTTPServer [port]
在Python 3中,执行python -m http.server [port]
这将在localhost:[port]上设置一个服务器。然后启动浏览器,导航到您的页面,看看问题是否消失。

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