Javascript 访问属性“文档”的权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13758708/
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
Permission denied to access property 'document'
提问by user1884702
I am using following script there, form this script on my page add iframe and I want to get iframe html.
我在那里使用以下脚本,在我的页面上形成这个脚本添加 iframe,我想获得 iframe html。
<script src="http://ads.sonobi.com/ttj?id=881134" type="text/javascript"></script>
Problem is in iframe there is inner iframe.
问题是在 iframe 中有内部 iframe。
Javascript following function using for get content of ifrmae
用于获取 ifrmae 内容的 Javascript 以下函数
document.getElementById('f1').contentWindow.document.body.innerHTML
When I run this will show following error
当我运行这将显示以下错误
Permission denied to access property 'document'
How to solve the problem of permission denied.
如何解决权限被拒绝的问题。
回答by Mark Hughes
You can't.
你不能。
Why? It's a security feature to stop Cross-Site Scripting (XSS) attacks.
为什么?这是一种阻止跨站点脚本 (XSS) 攻击的安全功能。
For example, you have an iframe that loads www.facebook.com. If this security feature wasn't implemented, you could easily fetch data from that site. From cookies, to what content is on the page.
例如,您有一个 iframe 加载www.facebook.com. 如果未实施此安全功能,您可以轻松地从该站点获取数据。从 cookie 到页面上的内容。
There is more helpful information about this here: http://www.veracode.com/security/xss
这里有更多有用的信息:http: //www.veracode.com/security/xss
回答by I wrestled a bear once.
I didn't look at the JS, but you can't get HTML from an remote page with javascript. Try PHP's cURL.
我没有查看 JS,但是您无法使用 javascript 从远程页面获取 HTML。试试 PHP 的 cURL。
回答by Jonathan de M.
Try
尝试
var frame = document.getElementById('f1');
var doc = frame.contentWindow || frame.contentDocument;
var html = doc.document.body.innerHTML

