I want to call an ajax to other pages on the browser or tab close. When we reload tab or close tab it calls onbeforeUnload and onUnload events.
If I reload page either by pressing ctrl+r
or by pressing enter
in address bar it should reload page without any prompt and if I click close of browser or tab or I press ctrl+w
keys it should prompt that "Changes you made might be lost" and if user click leave it should close tab and call an ajax, else it should stay on page.
Can anyone help me with this?
Thanks in advance
Asked
Active
Viewed 560 times
1

Adam Miklosi
- 764
- 5
- 18
- 28

user9261589
- 11
- 1
-
If you are concerned about changes being lost on close, those changes would also be lost on reload. So no need to differenciate IMHO – yunzen Nov 21 '18 at 08:50
-
You could take a look at this post: https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing/3888938 – Craws Nov 21 '18 at 09:31
1 Answers
0
Most of the browsers intentionally block popups triggered in onbeforeunload, for known reasons.
But if you want to preserve users' data from erasing, you can engage window.localstorage property.

Max Kurtz
- 448
- 5
- 17
-
What exactly i need is as following: I am adding a record and take user to that record's page. If user click on save button on that page then it is fine, also when user refreshes page then also no issue, but if user close tab without clicking the save button, it should ask user that if they leave tab then their record will be removed, and if they click on leave button it must call ajax to remove that data, but it must not ask same on refresh. It should only ask user on tab or browser close – user9261589 Nov 23 '18 at 03:51
-
The thing you want is provided by browsers extensions which user installs by himself. Definitly, chrome.tabs API is in charge. I know your intentions are good, but imagine if someone can do the things you want from remote server? How many warning popup messages is enough? One, two or maybe they can go infinitely? In my opinion, the browser which allow such an opportunity will be really unpopular. One of the possible solutions - notify a user about possible data lost in advance. – Max Kurtz Nov 23 '18 at 10:53