Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parent iframe element from inner page without knowing id?

Let's imagine that 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 empty page, custom hash attribute always has a different value, both pages are on the same domain for security reasons, number and order of iframe elements is random.

My question is: Is there a way to get from inner page (test.html) using Javascript to its proper iframe element? Let's say, I'm in the third iframe's page and I need to get to its iframe element and alert() hash value (in this case "gg4v5e").

To be more specific. If you are familiar with Firefox's Firebug, it visualizes this situation like this:

<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" in order to get parent element (<iframe>) when I'm with my Javascript at <html> element in inner page?

like image 681
CZFox Avatar asked Jun 30 '10 17:06

CZFox


People also ask

Can an iframe get parent URL?

Yes, accessing parent page's URL is not allowed if the iframe and the main page are not in the same (sub)domain. However, if you just need the URL of the main page (i.e. the browser URL), you can try this: var url = (window. location !=

How do I transfer data from iframe to parent?

Sending some data from the child iframe to the parent window is also pretty simple. Whenever you embed an iframe, the iframe will have a reference to the parent window. You just need to use the PostMessage API to send data via the window. parent reference of the parent window.

Will iframe work if JavaScript is disabled?

If a user has javascript disabled, iframes will work. An iframe tag has attributes “height” and “width,” which allows the designer great latitude with dimensions and format like 300×250 , 728×90 depending on the Ad size. Iframe tag can appear anywhere on the page and several iframes can be added if wished to.


1 Answers

Sure there is. This code works in FF and IE6, Opera, Chrome (requires a web server and both files coming from same domain 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"));
              }
           }
        }
like image 190
naikus Avatar answered Oct 26 '22 16:10

naikus