invalid return error...calling function?

Discussion in 'Javascript' started by evan.cooch@NOSPAMcornell.edu, Jan 5, 2004.

  1. Guest

    Greetings.

    Suppose I have some function called "CheckIt" - some function to
    validate form data before submitting it to e CGI script. Pretend the
    name of the form is "TheForm".

    If I use the following code, everything work perfctly:

    <Input type=button VALUE="Submit your form"
    onClick="javascript:return CheckIt(TheForm)">

    But, if I try to associate the submission with a graphic (call it
    "submit_botton.gif"), using the following, I get an error:

    <a href="javascript:return CheckIt(AbsForm)">
    <img src="images/submit_button.gif" border=0></a>


    the error message I get is:

    "invalid return"

    Using the Javascript console in Mozilla, it points to line 1, which is
    indicated to be

    javascript:return CheckIt(AbsForm)


    I've tried several things, to no avail. I'm puzzled, because I've used
    similar approaches (to associating functions with images), without any
    problems.

    Any suggestions? Pointers to the "obvious mistake"? :)

    Thanks in advance...
    , Jan 5, 2004
    #1
    1. Advertising

  2. Lee Guest

    said:
    >
    >Greetings.
    >
    >Suppose I have some function called "CheckIt" - some function to
    >validate form data before submitting it to e CGI script. Pretend the
    >name of the form is "TheForm".
    >
    >If I use the following code, everything work perfctly:
    >
    ><Input type=button VALUE="Submit your form"
    >onClick="javascript:return CheckIt(TheForm)">
    >
    >But, if I try to associate the submission with a graphic (call it
    >"submit_botton.gif"), using the following, I get an error:
    >
    ><a href="javascript:return CheckIt(AbsForm)">
    ><img src="images/submit_button.gif" border=0></a>
    >
    >
    >the error message I get is:
    >
    >"invalid return"


    There is a major difference between the value of an onclick attribute
    and the value of an href attribute. The onclick attribute is expected
    to be executable code.
    The token "javascript:", if present, is read as either a language
    specification or as a label, depending on what the browser is expecting.

    On the other hand, the href attribute is supposed to be an URI.
    The token "javascript:", if present, is read as a protocol name.

    The javascript [pseudo]protocol means that the browser should evaluate
    the javascript expression following the colon, and use its value as
    the new contents of the page, as in:

    href="javascript:'<html><body>'+(new Date()).toLocaleString()+'</body></html>'"

    Whatever value you're returning isn't valid HTML (or void, which means
    to leave the current page unchanged).

    In general, don't use the javascript: protocol. Use the onclick event
    handler of the link, and return false, so the link isn't followed.

    You should also investigate these concepts:
    onSubmit
    input type="submit"
    input type="image"
    Lee, Jan 5, 2004
    #2
    1. Advertising

  3. Guest

    THanks very much!

    My followup embedded below:


    >There is a major difference between the value of an onclick attribute
    >and the value of an href attribute. The onclick attribute is expected
    >to be executable code.
    >The token "javascript:", if present, is read as either a language
    >specification or as a label, depending on what the browser is expecting.
    >
    >On the other hand, the href attribute is supposed to be an URI.
    >The token "javascript:", if present, is read as a protocol name.
    >
    >The javascript [pseudo]protocol means that the browser should evaluate
    >the javascript expression following the colon, and use its value as
    >the new contents of the page, as in:
    >
    >href="javascript:'<html><body>'+(new Date()).toLocaleString()+'</body></html>'"
    >
    >Whatever value you're returning isn't valid HTML (or void, which means
    >to leave the current page unchanged).


    OK, so why then DOES the following worK

    <a href="javascript:void(document.TheForm.reset())">
    <img src="images/start_over.gif"></a>

    It uses href, but it executes the javascript just as intended.


    >
    >In general, don't use the javascript: protocol. Use the onclick event
    >handler of the link, and return false, so the link isn't followed.
    >
    >You should also investigate these concepts:
    >onSubmit
    >input type="submit"
    >input type="image"



    I'll do that, but would be interested in your answer to why the href
    approach works in some cases, but not others.
    , Jan 5, 2004
    #3
  4. Lee Guest

    said:
    >
    >THanks very much!
    >
    >My followup embedded below:
    >
    >
    >>There is a major difference between the value of an onclick attribute
    >>and the value of an href attribute. The onclick attribute is expected
    >>to be executable code.
    >>The token "javascript:", if present, is read as either a language
    >>specification or as a label, depending on what the browser is expecting.
    >>
    >>On the other hand, the href attribute is supposed to be an URI.
    >>The token "javascript:", if present, is read as a protocol name.
    >>
    >>The javascript [pseudo]protocol means that the browser should evaluate
    >>the javascript expression following the colon, and use its value as
    >>the new contents of the page, as in:
    >>
    >>href="javascript:'<html><body>'+(new Date()).toLocaleString()+'</body></html>'"
    >>
    >>Whatever value you're returning isn't valid HTML (or void, which means
    >>to leave the current page unchanged).

    >
    >OK, so why then DOES the following worK
    >
    ><a href="javascript:void(document.TheForm.reset())">
    ><img src="images/start_over.gif"></a>
    >
    >It uses href, but it executes the javascript just as intended.


    Because the Javascript expression evaluates to void, which means to
    leave the current page unchanged.
    Lee, Jan 5, 2004
    #4
  5. Guest


    >>OK, so why then DOES the following worK
    >>
    >><a href="javascript:void(document.TheForm.reset())">
    >><img src="images/start_over.gif"></a>
    >>
    >>It uses href, but it executes the javascript just as intended.

    >
    >Because the Javascript expression evaluates to void, which means to
    >leave the current page unchanged.



    Got it - thanks....
    , Jan 5, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Lagemann
    Replies:
    8
    Views:
    477
    Mike Wahler
    Jan 10, 2005
  2. tiwy
    Replies:
    0
    Views:
    438
  3. Patrick.O.Ige
    Replies:
    1
    Views:
    1,952
    Patrick.O.Ige
    Jul 2, 2006
  4. Greenhorn
    Replies:
    15
    Views:
    811
    Keith Thompson
    Mar 6, 2005
  5. Emil
    Replies:
    2
    Views:
    82
Loading...

Share This Page