PDA

View Full Version : Getting innerhtml of iframe


LucaUWF
02-07-2008, 03:45 PM
Hi,

I am nOOb to Javascript and wanted to extract HTML from another website.
I tried this by using an iframe that opened up the desired webpage and called a script to read the innerhtml of the frame using the onLoad event.

It fails however with a Permission Denied error when trying to extract the html via the script (see code below).

Not sure if I am doing something worng, or if this is just not allowed on ClientSide scripting (I can appreciate writing isn't) but not being able to read.

Anyway, assuming this can't be done in this manner, is there another way in which it might short of using server side scripting?

Thanks in advance


<SCRIPT language = "Javascript">
function readFile()
{
doc=window["myframe"].document.body.innerHTML;
//TBD...

}
</SCRIPT>

A1ien51
02-08-2008, 03:24 PM
You can not read the contents from other domains. JavaScript has the same domain policy so it protects you from people reading/changing content from sites such as banks, payment systems, email, etc.

So you are not going to be able to do it with JavaScript.

Eric

rnd me
02-09-2008, 06:13 PM
A site which has a generous crossdomain.xml (http://www.crossdomainxml.org/) policy, offers its content to flash.

this blog post (http://blog.monstuff.com/archives/000294.html) describes a way to fetch some sites without server-side scripting by providing a flash-based, yet javascript reachable ajax object. it basically allows GET and POST requests to 3rd party sites that support it by posting a small text file on their server. The linked example is a bit cumbersome, needing a dojo installation and about 8 script files, but theydo provide everything to make it work.

While news sites and search engines aren't likely to share their IP, many sites like the national weather service (http://forecast.weather.gov/crossdomain.xml), do indeed.

Also keep in mind you you can pull in RSS feeds from any site using yahoo pipes. i can post an example of that if you are interested. This is a much simpler operation, and it requires no external files, just about a dozen or so lines of code.