Capturing the browser tab reload and tab close functionality












0














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










share|improve this question
























  • If you are concerned about changes being lost on close, those changes would also be lost on reload. So no need to differenciate IMHO
    – HerrSerker
    Nov 21 '18 at 8:50












  • You could take a look at this post: stackoverflow.com/questions/3888902/…
    – Craws
    Nov 21 '18 at 9:31
















0














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










share|improve this question
























  • If you are concerned about changes being lost on close, those changes would also be lost on reload. So no need to differenciate IMHO
    – HerrSerker
    Nov 21 '18 at 8:50












  • You could take a look at this post: stackoverflow.com/questions/3888902/…
    – Craws
    Nov 21 '18 at 9:31














0












0








0







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










share|improve this question















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







javascript reload onbeforeunload onunload window.onunload






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 9:23









Adam

65521022




65521022










asked Nov 21 '18 at 8:33









user9261589

1




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
    – HerrSerker
    Nov 21 '18 at 8:50












  • You could take a look at this post: stackoverflow.com/questions/3888902/…
    – Craws
    Nov 21 '18 at 9:31


















  • If you are concerned about changes being lost on close, those changes would also be lost on reload. So no need to differenciate IMHO
    – HerrSerker
    Nov 21 '18 at 8:50












  • You could take a look at this post: stackoverflow.com/questions/3888902/…
    – Craws
    Nov 21 '18 at 9:31
















If you are concerned about changes being lost on close, those changes would also be lost on reload. So no need to differenciate IMHO
– HerrSerker
Nov 21 '18 at 8:50






If you are concerned about changes being lost on close, those changes would also be lost on reload. So no need to differenciate IMHO
– HerrSerker
Nov 21 '18 at 8:50














You could take a look at this post: stackoverflow.com/questions/3888902/…
– Craws
Nov 21 '18 at 9:31




You could take a look at this post: stackoverflow.com/questions/3888902/…
– Craws
Nov 21 '18 at 9:31












1 Answer
1






active

oldest

votes


















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.






share|improve this answer





















  • 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 3: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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407998%2fcapturing-the-browser-tab-reload-and-tab-close-functionality%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer





















  • 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 3: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
















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.






share|improve this answer





















  • 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 3: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














0












0








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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 9:39









Max Kurtz

987




987












  • 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 3: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


















  • 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 3: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
















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 3:51






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 3: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




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


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407998%2fcapturing-the-browser-tab-reload-and-tab-close-functionality%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen