Redirecting in ASP.NET MVC using Javascript or something else the best way?

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?

Answers


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.


window.location is a property not a method:

if (answer)
    window.location = "http://www.google.com";
else
    window.location = "http://www.yahoo.com";

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");
 }

Need Your Help

Fast operations on IEnumerable?

c# silverlight windows-phone

I have a Windows Phone project that requires searching through a database of around 70,000 XML entries, selecting those items that match a query, and returning those items to be displayed in a list...

Multiple reads from multiple C++ sockets

c++ sockets tcp

I'm working to implement a server for a Rock Paper Scissors protocol. So far things are going really well except I have one snag I'm trying to get past. As a total overview of the program: