Advanced Search

Results 1 to 6 of 6

Thread: Multiple onclick events

  1. #1
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple onclick events

    Hi all,

    Can anyone tell me why this doesn't work:

    <input type="button" onclick="return confirm('Are you sure?'); window.location='http://www.google.com'" value="GOOGLE"/>

    The first half works (i.e. the confirm box comes up), but when the user presses 'OK' nothing happens.

    Your superior knowledge in this regards and kind help is most appreciated.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Because you return from the event handler function with the first statement. In fact, using either of those statements means that the other will never get called, as using the second one first will navigate away from the page before the other is called. Try this:
    Code:
    onclick="if(confirm('Are you sure?')) window.location='http://www.google.com';"
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks! Your solution worked and i'm a happy customer.

    But...

    what i posted was an abbreviation for (what i believe to be the same thing).

    The original in my code is this functiom:

    function confirmSubmit(msg)
    {
    var agree=confirm(msg);
    if (agree) return true;
    else return false;
    }

    Being called like this:

    <input type="button" onclick="return confirmSubmit('If you continue you'll go to google.'); window.location='http://www.google.com/'" value="GOOGLE"/>

    Like i said, your code works, but i'd really like to know why my one (outlined above) doesn't.

  4. #4
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Deleted

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It is the same thing. That function is fine. The problem was that you used a return statement in your onmouseover event before the rest of the code. return immediately exits from the function it's called from.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the help.

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
  •