How to redirect a user to a new webpage after a Javascript Alert/confrim box
- by David Maldonado
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.