How to redirect a user to a new webpage after a Javascript Alert/confrim box

Posted by David Maldonado on Stack Overflow See other posts from Stack Overflow or by David Maldonado
Published on 2012-04-10T16:26:24Z Indexed on 2012/04/10 17:29 UTC
Read the original article Hit count: 229

Filed under:
|

I have a client who wishes to have an alert/confirm box pop up when a user leaves the site, then based on what they choose, they will either stay on the page or go to a new page (would love if it would work in all browsers).

I have been twiddling all day and have got this piece of code, but doesn't work too well.

<script>
window.onbeforeonload = function exitLeave(){var answer = confirm("You have not filled out your questionnaire yet")
if (answer){
    window.location = "http://www.google.com/";
}
else{
    alert("Cancel it !")
}    
} 
</script>

Any help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about redirect