in Search
     
Latest post 02-25-2007 9:31 PM by modchip. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 02-25-2007 8:58 PM

    • modchip
    • Top 10 Contributor
    • Joined on 02-09-2006
    • Ivalice
    • Posts 710
    • Points 10,110

    Accessing Javscript Variables From an iFrame

    Hi to all.

    Does anyone have any idea how to access variables from an iframe. The variable will be accessed from the parent window.

    EX:
    iframe.htm
    <HTML>
    <HEAD>

        <SCRIPT>var myvar="test";</SCRIPT>
    </HEAD>
    <BODY>

    ...
    </HTML>
    window.htm
    <HTML>
    ...
    <BODY>
    <IFRAME SRC="iframe.htm" name="myframe"><IFRAME>
    <SCRIPT>alert(//script to access the variable my var...);</SCRIPT>
    ...
    </HTML>
    Thanks for your time guys. Chao!

    • Post Points: 20
  • 02-25-2007 9:03 PM In reply to

    • cruizer
    • Top 10 Contributor
    • Joined on 12-14-2005
    • Singapore
    • Posts 944
    • Points 22,590

    Re: Accessing Javscript Variables From an iFrame

    Answer
    i'm not sure if your window.htm can access the variable via the reference myframe.myvar

    what i would do, though, is to put in the value inside a hidden form widget (input type="hidden" name="whatever" />) and access the value via myframe.document.<formName>.whatever.value (or myframe.document.forms[0].whatever.value if you are sure that the hidden input widget is in the first form in the document)

    http://devpinoy.org/blogs/cruizer
    Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
    • Post Points: 20
  • 02-25-2007 9:07 PM In reply to

    • modchip
    • Top 10 Contributor
    • Joined on 02-09-2006
    • Ivalice
    • Posts 710
    • Points 10,110

    Re: Accessing Javscript Variables From an iFrame

    Hey, that was fast!

    Thanks cruizer, it worked well! :D Thank you very much.

    <Problem solved!>

    • Post Points: 20
  • 02-25-2007 9:15 PM In reply to

    • cruizer
    • Top 10 Contributor
    • Joined on 12-14-2005
    • Singapore
    • Posts 944
    • Points 22,590

    Re: Accessing Javscript Variables From an iFrame

    i got curious so i tried it. yep myframe.myvar actually worked Smile
    http://devpinoy.org/blogs/cruizer
    Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
    • Post Points: 20
  • 02-25-2007 9:31 PM In reply to

    • modchip
    • Top 10 Contributor
    • Joined on 02-09-2006
    • Ivalice
    • Posts 710
    • Points 10,110

    Re: Accessing Javscript Variables From an iFrame

    Yep! How stupid I am... I had a typo alert(myframe.myvar;) -- should be alert(myframe.myvar);, sorry, my bad. frames['myframe'].myvar also works. Damn semi-colon. Zip it!

    Thanks again cruizer!

    • Post Points: 5
Page 1 of 1 (5 items)