View Full Version : Multiple onclick events

03-29-2006, 05:52 PM
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.

03-29-2006, 06:05 PM
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:
onclick="if(confirm('Are you sure?')) window.location='http://www.google.com';"

03-29-2006, 06:29 PM
Thanks! Your solution worked and i'm a happy customer.


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.

03-29-2006, 06:31 PM

03-29-2006, 07:01 PM
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.

03-30-2006, 05:01 PM
Thanks for the help.