4

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.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
user1884702
  • 41
  • 1
  • 1
  • 3

3 Answers3

4

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

Mark Bell
  • 28,985
  • 26
  • 118
  • 145
Mark Hughes
  • 418
  • 3
  • 16
1

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

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
-1

Try

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

Source: Frame/IFrame contentDocument Property.

bluish
  • 26,356
  • 27
  • 122
  • 180
Jonathan de M.
  • 9,721
  • 8
  • 47
  • 72