Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.onbeforeunload in Chrome: what is the most recent fix?

Obviously, window.onbeforeunload has encountered its fair share of problems with Chrome as I've seen from all the problems I've encountered. What's the most recent work around?

The only thing I've got even close to working is this:

window.onbeforeunload = function () { return "alert" }; 

However, if I substitute return "alert" with something like alert("blah"), I get nothing from Chrome.

I saw in this question that Google purposefully blocks this. Good for them... but what if I want to make an AJAX call when someone closes the window? In my case, I want to know when someone has left the chatroom on my website, signalled by the window closing.

I want to know if there's a way to either
(a): fix the window.onbeforeunload call so that I can put AJAX in there
or
(b): get some other way of determining that a window has closed in Chrome

like image 791
varatis Avatar asked Mar 08 '12 22:03

varatis


People also ask

What triggers 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.

What is the difference between Onbeforeunload and Onunload?

onbeforeunload Below are my findings on the iPad; Using window. onunload , I am able to get an alert when user navigates to a different page from myPage. html (either by clicking on some link or doing a Google search while on myPage.

What does window Onbeforeunload do?

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.


2 Answers

Answer:

$(window).on('beforeunload', function() {     var x =logout();     return x; }); function logout(){         jQuery.ajax({         });         return 1+3; } 

A little mix and match, but it worked for me. The 1+3 makes sure that the logout function is being called (you'll see 4 if it's successful on the popup when you try to leave).

like image 139
varatis Avatar answered Oct 12 '22 11:10

varatis


As of 69.0.3497.92, Chrome has not met the standard. However, there is a bug report filed, and a review is in progress.

  • Chrome requires returnValue to be set by reference to the event object, not the value returned by the handler.
  • The standard states that prompting can be controlled by canceling the event or setting the return value to a non-null value.
  • The standard states that authors should use Event.preventDefault() instead of returnValue.
  • The standard states that the message shown to the user is not customizable.

window.addEventListener('beforeunload', function (e) {      // Cancel the event as stated by the standard.      e.preventDefault();      // Chrome requires returnValue to be set.      e.returnValue = '';  });        window.location = 'about:blank';
like image 23
Trevor Karjanis Avatar answered Oct 12 '22 11:10

Trevor Karjanis