最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get parent iframe element from inner page without knowing id? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Mar 6, 2012 at 12:42 Waynn Lue 11.4k8 gold badges52 silver badges77 bronze badges asked Jun 30, 2010 at 17:14 CZFoxCZFox 9,2754 gold badges26 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17

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"));
              }
           }
        }
发布评论

评论列表(0)

  1. 暂无评论