Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'beforeunload' Chrome Issue

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

like image 504
skos Avatar asked May 21 '12 06:05

skos


People also ask

What triggers Beforeunload?

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.

What is the difference between unload and Beforeunload?

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.

What is Onbeforeunload?

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.


2 Answers

Return a string instead:

$(window).on('beforeunload', function(){
    return "Good bye";
});​
like image 130
Sampson Avatar answered Sep 22 '22 13:09

Sampson


Try this for all Browsers:-

window.addEventListener("beforeunload", function (e) {

  var confirmationMessage = "\o/";     
  e.returnValue = confirmationMessage;           
  return confirmationMessage;       

});
like image 24
Abhishek Salgaonkar Avatar answered Sep 25 '22 13:09

Abhishek Salgaonkar