I know there are a lot of questions regarding this but nothing is answering me right. I want to show a confirmation dialog when user leaves the page. If the user press Cancel he will stay on page and if OK the changes that he has made will be rollback-ed by calling a method. I have done like this:
window.onbeforeunload = function () {
var r = confirm( "Do you want to leave?" );
if (r == true) {
//I will call my method
}
else {
return false;
}
};
The problem is that I am getting the browser default popup: "LeavePage / StayOnPage"
This page is asking you to confirm that you want to leave - data you have entered may not be saved.
This message is shown in Firefox, in Chrome is a little different. I get this popup after I press OK on my first confirmation dialog.
Is there a way not to show this dialog? (the second one, that I did not create). Or if there is any way to control this popup, does anyone know how to do that? Thanks
I know there are a lot of questions regarding this but nothing is answering me right. I want to show a confirmation dialog when user leaves the page. If the user press Cancel he will stay on page and if OK the changes that he has made will be rollback-ed by calling a method. I have done like this:
window.onbeforeunload = function () {
var r = confirm( "Do you want to leave?" );
if (r == true) {
//I will call my method
}
else {
return false;
}
};
The problem is that I am getting the browser default popup: "LeavePage / StayOnPage"
This page is asking you to confirm that you want to leave - data you have entered may not be saved.
This message is shown in Firefox, in Chrome is a little different. I get this popup after I press OK on my first confirmation dialog.
Is there a way not to show this dialog? (the second one, that I did not create). Or if there is any way to control this popup, does anyone know how to do that? Thanks
Share edited Aug 14, 2015 at 5:38 Elyor 5,53210 gold badges54 silver badges78 bronze badges asked Oct 4, 2013 at 10:56 Noah MartinNoah Martin 1,80912 gold badges36 silver badges73 bronze badges 5- possible duplicate of How can I override the OnBeforeUnload dialog and replace it with my own? – Curtis Commented Oct 4, 2013 at 10:58
- 1 Thanks but please read for one moment my whole question, I have read all the other questions here before posting this – Noah Martin Commented Oct 4, 2013 at 11:00
- I am getting a problem! Can you help me with that? – Noah Martin Commented Oct 4, 2013 at 11:01
- 2 The answer to that question is the answer to yours. You may not like the oute, but it's still the only answer. – Stephan Muller Commented Oct 4, 2013 at 11:07
-
Previous versions of Firefox was letting to override
window.onbeforeunload
.. – asdf_enel_hak Commented Oct 4, 2013 at 11:25
3 Answers
Reset to default 14Here's what I've done, modify to fit your needs:
// This default onbeforeunload event
window.onbeforeunload = function(){
return "Do you want to leave?"
}
// A jQuery event (I think), which is triggered after "onbeforeunload"
$(window).unload(function(){
//I will call my method
});
Note: it's tested to work in Google Chrome, IE8 and IE10.
This is simple. Just use
window.onbeforeunload = function(){
return '';
};
to prompt when the user reloads, and
window.close = function(){
return '';
};
to prompt when the user closes the page.
But the user have to click on the page once, or do anything on the page for the code to detect. You don't have to put anything the the return'';
, because JavaScript interpreter would just ignore it.
window.onbeforeunload = function() {
if (data_needs_saving()) {
return "Do you really want to leave our brilliant application?";
} else {
return;
}
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743526524a4466903.html
评论列表(0条)