I'm trying to alert something out after closing page.
A simple window.unload
example as below :
HTML
<html>
<body>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="test.js" type="text/javascript">
</html>
test.js
$(window).unload( function () {
alert("Bye now!");
});
P.S :
I have tried javascript too, but doesn't alert anything out !
test.js
window.onunload = function() {
alert("Bye now!");
};
Most browsers prevent alert
in unload
. The best you can do is to use an onbeforeunload
handler that returns a string - the browser will show that string to the user:
window.onbeforeunload = function() {
return "Bye now!";
};
Demo here.
What browser are you testing this code in ?
If you check the following link from W3School, Opera and Chrome do not support it. LINK
And a working example for onbeforeunload is to use jquery as following :
$(window).on('beforeunload', function() {
// Do stuff
});
(I was going to comment this last bit on the above post but I cannot yet comment :'( )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With