Permission denied to access property 'document

2019-02-23 17:30发布

This question already has an answer here:

I am using following script there, form this script on my page add iframe and I want to get iframe html.

<script src="http://ads.sonobi.com/ttj?id=881134" type="text/javascript"></script> 

Problem is in iframe there is inner iframe.

Javascript following function using for get content of ifrmae

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.

3条回答
孤傲高冷的网名
2楼-- · 2019-02-23 17:44

You can't.

Why? It's a security feature to stop Cross-Site Scripting (XSS) attacks.

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.

There is more helpful information about this here: http://www.veracode.com/security/xss

查看更多
对你真心纯属浪费
3楼-- · 2019-02-23 18:04

Try

var frame = document.getElementById('f1');
var doc = frame.contentWindow || frame.contentDocument;
var html = doc.document.body.innerHTML

Source: Frame/IFrame contentDocument Property.

查看更多
forever°为你锁心
4楼-- · 2019-02-23 18:05

I didn't look at the JS, but you can't get HTML from an remote page with javascript. Try PHP's cURL.

查看更多
登录 后发表回答