neon
neon

Reputation: 1503

Javascript: Can I access source of the file that is loaded in the hidden iframe

I have found this code for putting hidden iframe into my html and loading url into it:

var i = document.createElement('iframe');
i.style.display = 'none';
i.onload = function() { i.parentNode.removeChild(i); };
i.src = 'http://www.google.com';
document.body.appendChild(i);

And I would like to ask, can I access in javascript the source code of the google.com loaded in this iframe, please?

Upvotes: 0

Views: 940

Answers (3)

Joe Awungshi
Joe Awungshi

Reputation: 1

http://www.dyn-web.com/tutorials/iframes/ - Try this one it helps Joe AWungshi

Upvotes: 0

elzapp
elzapp

Reputation: 1991

If the page in the iframe is hosted on the same hostname as the "parent" page, you could access the DOM of the child page from the parent page using JavaScript.

If the childpage is at another hostname, the security features of JS will prevent that.

Upvotes: 1

Fenton
Fenton

Reputation: 251072

The security features of an iframe will prevent you from accessing the source code inside the iframe using JavaScript. If you want to "screen scrape" you should do two things...

1) Check that it's okay for you to use the data you are scraping.

2) Use a server-side script to load and parse the page.

Upvotes: 1

Related Questions