1

I have developed a simple chat application where I am using the $window.onbeforeunload to notify other users when somebody closes the tab/browser (basically when the user leaves the room).

Here is my code

$scope.onExit = function() {
        $scope.chatstatus.$add({
            status: $scope.getUsername + ' left'    
        });
    };

    $window.onbeforeunload =  $scope.onExit;

This is working absolutely fine on desktop browsers but not on the Android Chrome browser. The onbeforeunload function is not getting triggered at all.

Please suggest a solution/workaround. Thanks.

EDIT: As mentioned in the comments that it is a known issue, kindly suggest a workaround if not the solution.

1 Answers1

0

Looks like you need to check for both onbeforeunload and onunload.

This answer looks like a good way to do that.

Android Chrome window.onunload

window.onunload = window.onbeforeunload = function() {
    var guid = sessionStorage.getItem("WindowGUID");
    var tmp = JSON.parse(localStorage.getItem("WindowGUIDs"));
    tmp = tmp.remove(guid);  // remove is a custom prototype fn
    localStorage.setItem("WindowGUIDs", JSON.stringify(tmp));
});
Community
  • 1
  • 1
Seano666
  • 2,238
  • 1
  • 15
  • 14
  • 1
    My mistake, I only got down to the "this works in Chrome" comment. It seems from searching that it is in fact a long-documented issue in mobile Chrome. https://bugs.chromium.org/p/chromium/issues/detail?id=339341 – Seano666 Mar 03 '16 at 18:20
  • Any way you could implement your own "close" functionality with an X button or some such button? Then have something on the server that checks at regular intervals if the client is connected. Or, the other way around the angular application checks regularly with your server. Just some random suggestions to get your functionality working. – Seano666 Mar 03 '16 at 19:15
  • Thanks. But, if the user chooses to close the browser tab instead of X button then again we will encounter the same issue. – Sachin Kariyattin Mar 04 '16 at 07:17
  • That's why I suggested checking to see if they are connected at regular intervals somehow. – Seano666 Mar 04 '16 at 14:33
  • I used window.onunload = window.onbeforeunload but did not work in Android Chrome. Other browsers are fine even IOS Chrome is also fine. – Meenakshi Khandelwal May 19 '21 at 20:14