Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Get value from iframe, "is null or not an object".

    Though I took out the source of the iframe, I get an error when I click the test button to get the value of msg_title. I am using IExplorer to debug it, as firefox doesnt show the error.

    Code:
    <script type='text/javascript'>
    function test(){
    page = window.location.href
    document.getElementById('1').value = window.frames[2].document.getElementByName('msg_title').value
    }
    function apple(){
    document.getElementById('3').innerHTML = '<iframe src="" name="4" id="4"></iframe>'
    }
    window.onLoad = test();
    </script>
    <div id='3' name='3'></div>
    <input type='button' value='apple' name='apple' id='apple' onclick='apple()'>
    <input type='button' value='test' name='test' id='test' onclick='test()'>
    <input type='text' value='' name='1' id='1'>
    <br>
    <iframe src='' name='2' id='2'>

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    document.getElementByName is not a function

    Firefox will show errors if you check the error console under 'Tools'.

    Identifiers should not begin with a digit.

    Code:
    window.onLoad = test();
    Event handlers are all lower case and expect to be assigned a function reference:

    Code:
    window.onload = test;
    I don't think that will fix all your problems.

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Yes, I put that code in there for who knows what reason. The error is with the window.frames['2'], though.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    As Logic Ali says, an element name or id may not start with a digit.

    It is your responsibility to die() if necessary….. - PHP Manual

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Let put it in other ways, as it looks like you understood nothing form what Logic Ali told you

    1. There is no getElementByName() method in JavaScript. There is getElementsByName(), and it returns a collection, not a single element. Or you may use getElementById() instead. name and id are not the same thing.

    2. name and id must start with a letter. Thus: id="2" and name="2" are HTML illegal.

    3. window.frames[2] means the second frame/iframe (counted from top to bottom) window.frames['2'] means nothing, as I said, because name can not start with a digit.

    4. window.frames[] is the reference of the iframe object, not the reference of the document loaded in the iframe.

    So:
    Code:
    document.getElementById('valid_Iframe_Id').contentWindow.document.getElementsByName('msgtitle')[0].value
    But it depends also on which kind of element has the name "msgtitle". If it is a form's element, it will work. If not, you'd better give that object an id, and use getElementById() instead.
    Last edited by Kor; 12-12-2010 at 07:40 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New Coder
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Yes, that seemed to fix it, but not I get "asdf is not defined.", and "apple is not a function.", added try{} because I got a permission error.

    Code:
    <script type='text/javascript'>
    function asdf(){
    page = window.location.href
    try
    {
    document.getElementById('b').contentWindow.document.getElementsByName('msg_title')[0].value
    }
    }
    function apple(){
    document.getElementById('d').innerHTML = '<iframe src="" name="c" id="c"></iframe>'
    }
    </script>
    <div id='d' name='d'></div>
    <input type='button' value='apple' name='apple' id='apple' onclick='apple()'>
    <input type='button' value='test' name='test' id='test' onclick='asdf()'>
    <input type='text' value='' name='a' id='a'>
    <br>
    <iframe src='' name='b' id='b'>
    Last edited by Toxhicide; 12-12-2010 at 07:56 AM.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    You may not use the same name apple as the name of a function and an element (button) name or id.

  • #8
    New Coder
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Issue solved.
    Last edited by Toxhicide; 12-12-2010 at 09:27 PM.


  •  

    Posting Permissions

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