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;
});
Related
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;
}
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 have an onberforeunload method which is correctly alerting the user when they are trying to close the tab or the browser using the follwoing code:
window.onbeforeunload = function(event) {
event.returnValue = 'Are you sure you want to leave/';
console.log(event.returnValue);
};
I have discovered that the onberforeunload is not being called when I click a hyperlink on the page. The warning message I want to display isn't appearing and the page is loading freely.
I have searched for many ways to create a popup for when a hyperlink has been selected but they all deal with one or groups of hyperlinks in div tags. I wish for a popup to display if any hyperlink is selected.
Why isn't onbeforeunload catching the exiting of the page through a hyperlink? Is my understanding of onbeforeunload wrong that it should be catching hyperlink exits?
UPDATE
I have updated the code to the following:
window.onbeforeunload = closeIT;
function closeIT() {
return 'here';
if(searchOnGoing){
return 'Ifs you leave the Youtube History page now the application will not finish';
}
};
It is still not working for hyperlinks and working for browser and tab closure. I am running this as part of a content script in a chrome extension which is injecting the content script into the page. Would this have an effect on it?
I also have an onunload following,i am wondering would this also have an effect on it?
window.onunload = function(event){
//Do something
}
Actually, it should trigger. Do you catch the event somehow via a click-event-listener? Anyway, you could show an alert with help of a click-event listener.
Following code-snippet is copied from a similar question: how to detect if a link was clicked when window.onbeforeunload is triggered?
Here the question was, in contrast, how to prevent the beforeunload event when links are clicked.
var link_was_clicked = false;
document.addEventListener("click", function(e) {
if (e.target.nodeName.toLowerCase() === 'a') {
link_was_clicked = true;
}
}, true);
window.onbeforeunload = function() {
if(link_was_clicked) {
link_was_clicked = false;
return;
}
//other code here
}
The following snippet correctly uses onbeforeunload.
<body onbeforeunload="return myFunction()">
Click me!
<script>
function myFunction() {
return "Please don't go!\nThis works beautifully!";
}
</script>
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 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?');
}