未捕获的SecurityError:阻止具有原点"null"的帧访问具有原点"null"的帧.协议,域和端口必须匹配

abc*_*d d 9 javascript iframe same-origin-policy

我在我的页面上设置了一个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";
}
Run Code Online (Sandbox Code Playgroud)

我在Chrome中收到2条错误消息:

  • 第一条错误消息:

    未捕获的SecurityError:阻止具有原点"null"的帧访问具有原点"null"的帧.协议,域和端口必须匹配.

    并指出错误:

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

    Uncaught SecurityError:无法从'HTMLIFrameElement'读取'contentDocument'属性:阻止具有原点"null"的帧访问具有原点"null"的帧.协议,域和端口必须匹配.

    它指出了以下错误消息:

    elem.contentDocument || elem.contentWindow.document : 
    
    Run Code Online (Sandbox Code Playgroud)

小智 -4

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