javascript - 获取属性访问权限被拒绝 "document"

标签 javascript html iframe

我不清楚为什么会收到此错误,当从不同的域在网络浏览器中加载 HTML 文件时,我没有在 HTTP 响应中设置任何 X-Frame-Options header

Permission denied to access property "document"

这是我的服务器头信息

curl -I zariga.com

输出

  HTTP/1.1 200 OK
Date: Wed, 13 Jul 2016 17:17:57 GMT
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Set-Cookie: JSESSIONID=656D65705C14A5B643B6EA281DF03A8D; Path=/
Vary: Accept-Encoding

我的 HTML iFrame 文件

<html>
<script>

function myFunction() {

    document.domain = "zariga.com";

var i = document.createElement('iframe');

i.setAttribute('id', 'i');
i.setAttribute('style', 'visibility:hidden;width:0px;height:0px;');
i.setAttribute('src', 'http://www.zariga.com/');
i.onload = function(){
alert(i.contentWindow.document.getElementsByName('syc')[0].value);
 alert(2);
};

document.body.appendChild(i);

}

</script>
<body>
<button onclick="myFunction()">Try it</button>
</body>

</html>

最佳答案

您无法通过这种方式访问​​ iframe 的内容。否则,您可以轻松注入(inject)其他网站并窃取访问者的帐户。

如果您有权访问 iframe 中包含的网站,则可以让它向包含它的页面发送消息。iframe 中的 window.parent 相当于主页中的 window。

因此,要发回值,请将此代码添加到主页:

function childCallback(value) {

}

并将其添加到 iframe 中的页面:

parent.childCallback(document.getElementsByName('syc')[0].value);

关于javascript - 获取属性访问权限被拒绝 "document",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38357909/

相关文章:

javascript - 为什么有2个setState可用

javascript - 如何检查一个文本框值是否应小于另一个文本框值,并且两个值的总和不应大于 1

database - (HTML 5) 太多的本地存储是多少?

javascript - 将 Div ID 添加到 YouTube iFrame

javascript - 触发父iframe中的点击事件时,如何让IFRAME作为父级监听滚动事件

javascript - JQuery Click 通过另一次点击的代码

javascript - 将变量作为请求对象的一部分而不是 JSP 中的表单字段发送

jquery - 想要摆脱 Bootstrap 导航栏上的细线

javascript - 约束验证和 knockout 验证

Facebook 点赞按钮需要 XHTML+RDFa,但它使用的 iframe 对该文档类型无效