qtjCH5pLao6
qtjCH5pLao6

Reputation: 43

How to take action when user closes mobile browser window?

I've been using these two, which mostly work well:

$(window).on('beforeunload', function() {});

$(window).on('unload', function() {});

I use SignalR, and these trigger a function on the server which removes a record in a database. However, investigating the database when the page is live reveals that some records do not get removed. I also know I've got quite a few mobile users. Could there be a problem with mobile browsers not triggering these events? I hope there is something I can do to fix this.

Upvotes: 1

Views: 604

Answers (1)

Kwame Opare Asiedu
Kwame Opare Asiedu

Reputation: 2345

The term "closes mobile browser window" can mean many things...

  1. The user actually closes the tab (or all tabs)...
  2. The user closes the browser app (by swiping the app off the recent apps or by closing all recent apps)...
  3. System closes browser process (due to low memory or some other reason)...

etc.

Not all these actions may trigger the beforeunload or unload events in the browser tab and unfortunately, you do not have any way to intercept some these actions

Your only viable option is to perhaps look into other ways of doing what you are trying to accomplish with the unload events...

Upvotes: 1

Related Questions