错误:访问属性“文档"的权限被拒绝 [英] Error: Permission denied to access property "document"

查看:28
本文介绍了错误:访问属性“文档"的权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 iframe 的 HTML 文档.每当我尝试使用 JS 访问或修改此 iframe 时,我都会收到 Error: Permission denied to access property "document".

I have a HTML Document which contains an iframe. Whenever I try to access or modify this iframe with JS I get Error: Permission denied to access property "document".

我正在使用 frame.contentWindow.document.body.innerHTMLframe.contentWindow.document.body.onload 或类似的此类属性来访问或修改 iframe.(在给定的代码中,iframe 被称为 frame.)

I am using frame.contentWindow.document.body.innerHTML or frame.contentWindow.document.body.onload or similar such attributes to access or modify the iframe. (In the given code the iframe is referred to as frame.)

对于我正在开发的网络应用程序,访问这些属性是必要的,我不能没有这些(或类似的替代品).

For the web-app I am developing, access to these attributes are necessary and I can't do without these (or similar alternatives).

推荐答案

即使您无权访问接收窗口的标题部分,您仍然可以在 YQL 的帮助下绕过此问题.使用 Postmessage 方法,您还需要编辑收件人窗口脚本.但是使用这种方法您可以加载任何 iframe 而无需触及它们的脚本.看看这个!

You can still bypass this issue with the help of YQL even though you don't have access to the header part of the receiving window. With the Postmessage method also you need to edit the recipient window script. But using this method you can load any iframe without touching their scripts. Check this out!

<html>
<iframe src="https://google.com/" width="500" height="300"></iframe>

<script>
var iframe = document.getElementsByTagName('iframe')[0];
var url = iframe.src;
var getData = function (data) {
    if (data && data.query && data.query.results && data.query.results.resources && data.query.results.resources.content && data.query.results.resources.status == 200) loadHTML(data.query.results.resources.content);
    else if (data && data.error && data.error.description) loadHTML(data.error.description);
    else loadHTML('Error: Cannot load ' + url);
};
var loadURL = function (src) {
    url = src;
    var script = document.createElement('script');
    script.src = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20data.headers%20where%20url%3D%22' + encodeURIComponent(url) + '%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=getData';
    document.body.appendChild(script);
};
var loadHTML = function (html) {
    iframe.src = 'about:blank';
    iframe.contentWindow.document.open();
    iframe.contentWindow.document.write(html.replace(/<head>/i, '<head><base href="' + url + '"><scr' + 'ipt>document.addEventListener("click", function(e) { if(e.target && e.target.nodeName == "A") { e.preventDefault(); parent.loadURL(e.target.href); } });</scr' + 'ipt>'));
    iframe.contentWindow.document.close();
}

loadURL(iframe.src);
</script>
</html>

这篇关于错误:访问属性“文档"的权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆