SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Help me javascript & iframe ??

    I write 2 file .htm like:
    ===1.htm===
    <iframe src="2.htm" id="ifm"></iframe>


    <script>

    function n()
    {
    y=ifm.document.getElementById("myHeader");
    alert(y.innerHTML);
    }
    </script>
    <input type="button" value="button2" onclick="n();" />
    ===2.htm===
    <a id="myHeader" src="#">Thu the </a>

    ======================================
    Resul : when I run 1.htm by IE, and I click "Button 2 ", will show alert "Thu the".

    I want to get text "thu the", with file 2.htm is file online like: "http://muangay123.com/2.htm" .
    HELP ME!
    Thanks All !!

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In the function you've written, you're using a variable "ifm" that is undefined. You're also polluting the global scope - you should really be using var to declare variables. That's part of the problem. The other part is that you can only access the contents of iframes via a special contentWindow object:

    Code javascript:
    function n() {
       var y = document.getElementByID('ifm').contentWindow.document.getElementById("myHeader");
       alert(y.innerHTML);
    }

    In the future, please use syntax highlighting for your code.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks u,
    I tried it again, but the your fucntion don't run .
    I want to get "Thu the" from http://muangay123.com/2.htm
    Like:
    <iframe src="http://muangay123.com/2.htm" id="ifm"></iframe>


    <script>

    function n()
    {
    var y=ifm.document.getElementById("myHeader");
    alert(y.innerHTML);
    }
    </script>
    <input type="button" value="button2" onclick="n();" />
    ===http://muangay123.com/2.htm===
    <a id="myHeader" src="#">Thu the </a>

    But the function don't run . This function run only when I run it is offline . Hix

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The two have to be on the same domain for JavaScript to be able to communicate between them.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •