Reputation: 1705
I need some help here:
I am writing code to redirect a user to a different page and execute some code, if the user closes the window while he is working on something:
<script type="text/javascript">
window.onbeforeunload = function() {
location.assign('http://www.somesite.com');
return "go?";
}
</script>
Now the code works fine if the user presses cancel. But if he presses Ok, the windows closes without a redirect.
I just need a working code that does one simple thing - redirect the user if the window is closed - can anyone suggest me the code for it
I know that most of you will say that I should not be doing this and I know that this is not the best practice. But if any one of you has written a successful piece of code, please share it here.
Upvotes: 0
Views: 204
Reputation: 2220
Actually you should not. All the browsers make sure you don't.
Even if you find a way, rest assured that it will be on every browser's bug list, next day morning.
If the user wants to close the window there is nothing you can do.
He will.
The only thing you can do is ask politely "Are you sure?"
Upvotes: 1