I have this simple piece of code -
$(window).bind('beforeunload', function(){
alert("Good Bye")
});
Works great with Firefox, IE8 but not in Chrome. Is it a known problem or is there any alternative for that ?
Actually what I am trying to do is to log details whenever user tries to close the browser.
function LogTime()
{
jQuery.ajax({
type: "POST",
url: "log.php",
data: "",
cache: false,
success: function(response)
{
}
);
}
$(window).bind('beforeunload', function(){
LogTime();
});
This works well in Firefox, but not in Chrome
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.
beforeunload event – the user is leaving: we can check if the user saved the changes and ask them whether they really want to leave. unload – the user almost left, but we still can initiate some operations, such as sending out statistics.
The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.
Return a string instead:
$(window).on('beforeunload', function(){
return "Good bye";
});
Try this for all Browsers:-
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
e.returnValue = confirmationMessage;
return confirmationMessage;
});
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