Referring iframes in javascript

by Vijay 2009-08-28 14:20:33

When we have some frames or iframes inside a page and we want to refer/access an iframe element from the main page or main window , then we can use
contentWindow.document property.


for example if we have a iframe with the id "myframe"

<iframe name="myframe" frameborder=0 id="myframe" width=798 height=290 marginheight=0 marginwidth=0 src="sample.php">


then we can access elements inside iframe using

document.getElementById(iframeId).contentWindow.document // works both in IE and firefox
(or)
document.getElementById(iframeId).contentDocument // works in DOM
(or)
window.frames[iframeName].document // works only if the iframe is already loaded by writing code in HTML


From the iframe , the main page or parent document can be reffered by "parent" property

Eg:

parent.document.getElementById('zz'); // will get the object from the parent window with the id 'zz'




Tagged in:

1096
like
0
dislike
0
mail
flag

You must LOGIN to add comments