Results 1 to 4 of 4

Thread: get value from inner iframe page

  1. #1
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default get value from inner iframe page

    how can i get value from inner iframe page, i have tried the bellow code but not working for me can some one help me out

    parent.htm
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <iframe id="myframe" src="child.htm"></iframe>
    
    
    
    <p ></p>
    
    <button onclick="myFunction()">Try it</button>
    
    <script>
    function myFunction()
    {
    var x = document.getElementById("myframe");
    
    var y = (x.contentWindow || x.contentDocument);
    
    if (y.document)
    {
    y = y.document;
    y.getElementsByTagName("input")[0].value;
    alert(y);
    }
    }
    </script>
    
    </body>
    </html>
    child.htm
    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body bgcolor="#000000">
    <form action="" method="post" name="myform" id="myform">
    <table width="200" border="1">
      <tr>
        <td width="81" align="left">firstname</td>
        <td width="103"><input type="text" name="fname" value="John" id="fname" /></td>
      </tr>
      <tr>
        <td align="left">lastname</td>
        <td><input type="text" name="textfield2" value="petter" /></td>
      </tr>
    </table>
    
    </form>
    </body>
    </html>
    Last edited by jscheuer1; 05-12-2014 at 12:54 PM. Reason: format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Two things. First you are not asking it to alert the value, you are asking it to alert the document:

    Code:
    if (y.document)
    {
    y = y.document; //last assignment to y is the document.
    y.getElementsByTagName("input")[0].value; //this does nothing, it's like saying "John" to the interpreter, but nothing is assigned.
    alert(y); //if the rest of the code is working, this alerts 'HTMLDocument" or similar, or there could be a type mismatch and no alert.
    }
    If you want to alert the value of the input as indicated, do:

    Code:
    if (y.document)
    {
    y = y.document;
    y = y.getElementsByTagName("input")[0].value;
    alert(y); //now this alerts "John"
    }
    Now it will work. That is unless the browser has any reason to think that these two pages are not of the same origin. When testing locally, for some browsers that means that they must both be in the same folder. There could be other problems with local testing. So it's best to test live. Once live, the only requirement is that both pages be on the same domain.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    parent.htm:
    Code:
    <!DOCTYPE html>
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>&nbsp;</title>
    <script>
    function myFunction()
    {
    var y=frames.myframe.document
    if (y)
    {alert(y.getElementsByTagName("input")[0].value);}
    }
    </script>
    </head>
    
    <body >
    <iframe name="myframe" src="child.htm"></iframe>
    <p ></p>
    <button onclick="myFunction()">Try it</button>
    </body>
    
    </html>

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Right, either way. Whether you're accessing the iframe as an element on the page (as you originally have), or as an instance of the window's frames object (as Arie does, and as is more direct in both his and my opinion - but it can be done either way), once you have a reference to the document of the page in the iframe, you still need to use the reference to the input's value in the alert in order to get what we think you're after.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. menu in iFrame that opens a page in the next iFrame
    By mikersson in forum JavaScript
    Replies: 0
    Last Post: 03-23-2010, 01:54 AM
  2. Replies: 0
    Last Post: 09-07-2009, 09:17 AM
  3. Replies: 2
    Last Post: 12-25-2008, 06:51 AM
  4. Replies: 0
    Last Post: 06-29-2008, 04:33 AM
  5. Replies: 0
    Last Post: 06-16-2005, 03:16 AM

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
  •