I want to create a custom modal window that will display when the user closes the Chrome Tab where the app is running (clicks the X) and says "Are you sure you want to close?". However, no matter what I have tried, I either keep getting the default Chrome "are you sure you want to leave this site" message, or it just closes the window with no modal showing up.
I have this code firing when the window is going to close:
window.onbeforeunload = function(e) {
e.preventDefault();
$('#myModal').show();
};
I have read that "onbeforeunload" doesn't work anymore, specifically from here: https://developers.google.com/web/updates/2017/03/dialogs-policy. I haven't gotten any of the alternatives to work though.
Is there something that I am not getting? I've been at this for hours now and have made no progress.
Try this:
window.onbeforeunload = function(e) {
alert();// your modal -> $('#myModal').show();
return false;
}
Related
I have a requirement where when we close the tab or close the browser I need to perform some processes. When user will close the tab, there should be no popup getting displayed. I should be able to directly trigger the Leave button of the popup. The code which I am using sometimes work and sometimes not. Is it possible to close the tab or browser without popup or the popup is mandatory. Please advise. Below is my code :-
Without Popup(sometimes work and sometimes not)
window.onbeforeunload = function() {
//debugger;
perform();
};
//With this i get a popup which I don't want
window.addEventListener("beforeunload", function (e) {
perform();
(e || window.event).returnValue = null;
return null;
});
I want to prevent a close tab or close browser in a web page.
I am using this code:
<script type="text/javascript">
window.onbeforeunload = function(e) { //Doesn't work well
return 'Exit';
};
$('#form').submit(function() { //No alert on submit form (this works)
window.onbeforeunload = null;
});
</script>
If I click somewhere in the page the code works fine and ask me if I really want to close the page, but if I reload the page and then I close the tab without click something in the page the tab close without show any alert.
Do you have some idea how to fix?
I want to clear local storage values when user close browser or window tab using in angularjs. I tried the following code.
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
alert("exit");
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome
});
In the above code it asks the confirmation alert messages when refresh the page and also close the page. But i want to fire angularjs event when close the browser/Window Tab only no need to ask the confirmation messages.
In the last project I worked on, I used the following code:
$window.onbeforeunload = function (event) {
if (self.form.$dirty) {
return 'You have made changes, but you did not save them yet.\nLeaving the page will revert all changes.';
}
}
First it performs a check to see if the data in the form has been changed. If so, when the user tries to close the window or go to another url, a popup will be shown stating that there are unsaved changes.
So in this event you have access to the controller, so all angular events should be able to fire.
This one worked for me, but you need to pay attention that the custom message doesn't work in most of the browsers, (such as chrome, IE, firefox).
window.onbeforeunload = () => {
return 'Are you sure you want to leave without saving?';
}
This will fired when the user refresh or close the tab or window, with the browser default message.
i have tried this code, it works for me!
window.onbeforeunload = close;
function close(){
// do something...
localStorage.clear();
sessionStorage.clear();
return null;
}
I want to show a confirmation dialog(Are you sure?) with 2 button "Yes" and "Cancel" before exit when the user press the native back button on the home(first) screen. And based on the buttons the decision should be made.
My question is where do I write the confirm dialog(/popup) code. I tried inside onExit(), but its not working.
Thanks.
You have to attach on the right browser resp. native events. Here are the best guesses I found:
How to prevent Android from closing web-application when backbutton is pressed?
detect back button click in browser
http://www.irt.org/script/311.htm
window.onbeforeunload = function (e) {
var e = e || window.event;
var msg = "Do you really want to leave this page?"
// For IE and Firefox
if (e) {
e.returnValue = msg;
}
// For Safari / chrome
return msg; };
That piece of code appears not to work on iOS, not sure about Android.
Another possibility would be to make your page open in a new window or use a redirect. This way your application would be the first page in window.history stack and there will be no back beyond your start page.
GL
Chris
I am trying to have a confirm box come up when the viewer tries to close the window, that allows the user to stay on the current page (by click Cancel), or continue closing the window (by click OK).
My code is as follows...
<script>
function confirm_exit(){
var message = window.confirm("My message.");
if (message == true) {
// Output when OK is clicked
window.close();
} else {
// Output when CANCEL is clicked
???????
}
}
</script>
I am not sure because either one I click closes the window. I need Cancel to remain at the current page, and OK to close the window or proceed with the users window.event.
Hope this makes sense.
Courtesy of a certain website with a dash in it's name:
window.onbeforeunload = leaveConfirm;
function leaveConfirm() {
var leaveThePageMessage = 'Are you sure you want to leave this page?';
return leaveThePageMessage;
}
This should work in all browsers (except for Opera).
Try:
window.onbeforeunload = function()
{
return confirm('Are you sure you want to close?');
}