Search code examples
javascriptiframe

How to get iFrame src content using javascript?


I have an iFrame, content of which is inside another iFrame. I want to get the inside iFrame's src content. How can I do this using javascript?


Solution

  • The outer page:

    <html>
    <body>
    <iframe id="ifr1" src="page1.htm"></iframe>
    <iframe id="ifr2" src="http://www.google.com"></iframe>
    </body>
    </html>
    

    page1.htm:

    <html>
    <body>
    <script type="text/javascript">
    document.write(parent.document.getElementById('ifr2').src);
    </script>
    </body>
    </html>