1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

onload error

Discussion in 'JavaScript' started by Weirfire, Nov 16, 2004.

  1. #1
    When you use the document.<formname>.submit function I get an error that states

    document.<formname> is null or not an object



    Anyone got any ideas how to get rid of this error? I'm expecting expat to know this 1 :)
     
    Weirfire, Nov 16, 2004 IP
  2. expat

    expat Stranger from a far land

    Messages:
    873
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    well we aim to please.....

    if it's regarding the bit of script I think you reffer to....

    onload <formname> object MUST exist the object is the actual form name used!

    .. echo"onLoad='document.SecureForm.submit();'";
    (watch the quotes and semikolons)

    ...echo "<form target=\"_self\" action=\"https://www.paypal.com/cgi-bin/webscr\" name=\"SecureForm\" method=\"post\">";

    M
     
    expat, Nov 16, 2004 IP
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    I have the form inside an IF clause which I think why it's causing the error because when it isn't being called it doesnt exist.

    hmmm.. I'll have to to think through the pseudocode :confused:
     
    Weirfire, Nov 16, 2004 IP
  4. expat

    expat Stranger from a far land

    Messages:
    873
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    just generate and else that runs the form to a standard out

    eg
    if gatway pp
    form with pp details
    else
    form to print or so (action print.php)
    end
     
    expat, Nov 16, 2004 IP
  5. phrozen_ra

    phrozen_ra Peon

    Messages:
    147
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    use this:

    instead
    
    document.<formname>
    
    Code (markup):
    use
    
    document.getElementById('<formname>')
    
    Code (markup):
     
    phrozen_ra, Jan 18, 2005 IP
  6. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Using getElementById with form name will only work in IE. If you want this to work in all browsers use form ID. If you want to use form name, you should use getElementsByName (this method returns a node list):

    document.getElementById("formid");
    document.getElementsByName("formname").item(0);
    Code (markup):
    J.D.
     
    J.D., Jan 18, 2005 IP
  7. phrozen_ra

    phrozen_ra Peon

    Messages:
    147
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    False, also works in Fireforx, Netscape, Opera...

    show me one browser used by more than 5% of the total users over the internet, which does not work well with getElementById
     
    phrozen_ra, Jan 18, 2005 IP
  8. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #8
    No it doesn't. Try this in IE and FF:

    <html>
    <head>
    <script type="text/javascript">
    function myonclick(input)
    {
    	alert(document.getElementById("formid").nodeName);
    	alert(document.getElementById("formname").nodeName);
    	alert(document.getElementsByName("formname").item(0).nodeName);
    }
    </script>
    </head>
    <body>
    <form id="formid" name="formname" action="formid.html">
    <input type="button" onclick="myonclick(this)" value="Click!">
    </form>
    </body>
    </html>
    Code (markup):
    ID and name are not interchangeable. There may be only one element with a given ID and there may be more than one with a given name.

    J.D.
     
    J.D., Jan 18, 2005 IP
  9. phrozen_ra

    phrozen_ra Peon

    Messages:
    147
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    it won't logicaly work.... and the bug is here:
    alert(document.getElementById("formname").nodeName);

    you don't have an id="formname" there.... why do you try to use it?

    it works only in IE, because IE is tought that way.... but logicaly you are calling a unknown element by an unexistent ID...

    It is somehow my bad cause I forgot to mention that <formname> must be be passed to id also...

    and what's bad in having distinct IDs?
     
    phrozen_ra, Jan 18, 2005 IP
  10. phrozen_ra

    phrozen_ra Peon

    Messages:
    147
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    furthermore if you would use CSS... you'd notice that distinct IDs help a lot ;)
     
    phrozen_ra, Jan 18, 2005 IP
  11. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Man, it's like you are reading between the lines. The rule is simple - use getElementById only with IDs and use getElementsByName with names, whatever they are. That's it. You quoted in your initial example getElementById used with the form name, which was incorrect. Even if your ID and name are the same, you should've used ID to avoid ambiguity.

    J.D.
     
    J.D., Jan 18, 2005 IP