Vishal
Vishal

Reputation: 12369

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?

Upvotes: 0

Views: 470

Answers (3)

Jeff Sternal
Jeff Sternal

Reputation: 48593

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.

Upvotes: 2

user240141
user240141

Reputation:

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

Upvotes: 0

stuartd
stuartd

Reputation: 73253

window.location is a property not a method:

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

Upvotes: 4

Related Questions