Capturing the browser tab reload and tab close functionality
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
add a comment |
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
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 8:50
You could take a look at this post: stackoverflow.com/questions/3888902/…
– Craws
Nov 21 '18 at 9:31
add a comment |
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
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
javascript reload onbeforeunload onunload window.onunload
edited Nov 21 '18 at 9:23
Adam
68531423
68531423
asked Nov 21 '18 at 8:33
user9261589user9261589
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
– yunzen
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
add a comment |
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 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
– yunzen
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
– yunzen
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
answered Nov 21 '18 at 9:39
Max KurtzMax Kurtz
1139
1139
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 8:50
You could take a look at this post: stackoverflow.com/questions/3888902/…
– Craws
Nov 21 '18 at 9:31