Access to cross-domain iframes using JavaScript

I have an iframe in my html page and I want to access some content from the page open in this iframe.

<table border="1" width="100%" height="100%"> <tr> <td id = "freebase_td"> <iframe id = "freebase_frame" src="http://www.freebase.com/view/en/angelina_jolie" width="100%" height="400px"></iframe> </td> </tr> </table> 

In this web page source

  <h1 id="page-title"> Angelina Jolie </h1> 

I want to access the content in the h1 tag with the id "page-title" using javascript. Is it possible?

+4
source share
1 answer

You should first read about browser policies with the same source code . This prevents the use of javascript from a frame or window or iframe in one source of access to content or scripts in another frame at another beginning. So, if your iframe does not match the source page of your script, it cannot directly access the contents of the iframe.

If you have one of the latest browsers, and you encode both frames for collaboration (which means you need to manage javascript code in both frames), a new window messaging function appears that can be used to transfer information between frames from different sources.

+4
source

All Articles