How to get parent iframe from inside page without knowing id?

Suppose I have something like this:

<html> ... <div> <iframe src="test.html" hash="r4d5f7"></iframe> <iframe src="test.html" hash="8f7x97"></iframe> <iframe src="test.html" hash="gg4v5e"></iframe> <iframe src="test.html" hash="e54f87"></iframe> </div> ... </html> 

test.html is a blank page, the hash user attribute always has a different value, both pages are in the same domain for security reasons, the number and order of iframes are random.

My question is: is there a way to get from the internal page (test.html) using Javascript in my own iframe element? Say I'm on the third page of an iframe, and I need to go to its iframe element and alert () the hash value (in this case "gg4v5e").

To be more specific. If you are familiar with Firefox Firebug, he visualizes this situation as follows:

 <html> ... <div> <iframe src="test.html" hash="r4d5f7"> <html> ... </html> </iframe> <iframe src="test.html" hash="8f7x97"> <html> ... </html> </iframe> <iframe src="test.html" hash="gg4v5e"> <html> ... </html> </iframe> <iframe src="test.html" hash="e54f87"> <html> ... </html> </iframe> </div> ... </html> 

Is it possible to call "something" to get the parent element ( <iframe> ) when I'm with my Javascript on the <html> element on the inner page?

+7
javascript jquery dom html iframe
source share
1 answer

Of course have. This code works in FF and IE6, Opera, Chrome (requires a web server and both files coming from the same protocol and port)

  function getHash() { var ifs = window.top.document.getElementsByTagName("iframe"); for(var i = 0, len = ifs.length; i < len; i++) { var f = ifs[i]; var fDoc = f.contentDocument || f.contentWindow.document; if(fDoc === document) { alert(f.getAttribute("hash")); } } } 
+16
source share

All Articles