get parent iframe element from inner page without knowing id

Parent html

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

iframe html

<html>
<script>
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"));
              }
           }
        }
</script>
	<body>
		<h1 id="goodid">good</h1>
	</body>
	<button onclick="getHash()">Click me</button>
</html>

This code works in FF and IE6, Opera, Chrome (requires a web server and both files coming from same domain protocol and port)

 

In my case, i need to use this in the gwt, so i have to replace the fDoc===document with fDoc===$wnd.document or $doc.

More detail in this POST.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s