views:

33

answers:

3

I have a confirm box and redirect to an action but it is not working..

<script type="text/javascript">


  function quitProgram()
    {
        var answer = confirm("Are you sure you want to quit your current program?");
        if (answer)
            window.location("http://www.google.com");
        else
            window.location("http://www.yahoo.com");
     }
    </script>

Html Code -

<input style="float:right;" type="submit" value="Quit Program" id="QuitProgram" onclick="quitProgram()" />

But the redirect never happens...can anyone help me with this..ultimately what i want to do is redirect to an action based on user response...it would be great if anyone lets me know the best way i should do this?

+4  A: 

window.location is a property not a method:

if (answer)
    window.location = "http://www.google.com";
else
    window.location = "http://www.yahoo.com";
Stuart Dunkeld
Nice.. it's like a brain teaser sometimes, when something looks right but is the totally wrong syntax.
Fosco
ya...my bad...and thanks Jeff...actually that submit was a problem..it worked when i changed it to button..
Misnomer
A: 

I am not sure but try this

function quitProgram()
{
    var answer = return confirm("Are you sure you want to quit your current program?");
    if (answer)
        window.location("http://www.google.com");
    else
        window.location("http://www.yahoo.com");
 }
Amit Ranjan
+2  A: 

Stuart identified one problem with the code posted in the question. One more thing you'll have to do is change the input element's type from "submit" to "button", or the submit will cause a post that will override the redirect.

Jeff Sternal