Preventing the browser to unload after clicking the close button [duplicate] - javascript

This question already has answers here:
Confirm message on browser page close
(2 answers)
Closed 5 years ago.
Preventing the browser to unload after clicking the close button. At the same time I want to display the popup window having "save" and "anotherFunction" buttons. Now, the popup window is displaying, but parent window is closed after clicking the "close" button in the browser. How to prevent the unloading?.
My Code:
window.onbeforeunload = function (e) {
openwindow(200, 100, 'Close.aspx');
};

onbeforeunload is a very strict event in which you cannot stop it from executing by no means. This is useful because if the developer of the site wanted to lock you in, what could you do?
If the assigned function to this event returns a non-void value, the user is shown a dialog box with the content (usually a string). If it returns a falsy value, no intervention happens.
Read your documentations, people.
https://developer.mozilla.org/en-US/docs/DOM/window.onbeforeunload
For your case, you are showing a html dialog box but since it cannot stop the page being closed it is rendering itself invalid.

Related

An event for when browser tab becomes active form inactive? [duplicate]

This question already has answers here:
Is there a way to detect if a browser window is not currently active?
(24 answers)
Closed 4 years ago.
Is there an event I can listen to when a browser tab becomes active.
By becoming active I mean all of the following things:
When user switches to the tab with my website form another tab.
When user switches back to the browser (with tab that contains my website open) from another App.
When user unminimizes the browser (with tab that contains my website open)
Basically when our tab becomes active from any other condition.
You could use the following event for this,
document.addEventListener("visibilitychange", function() {
console.log(`Your page is ${document.visibilityState}`);
});
You can check the browser compatibility of the above here.

How does stackoverflow create the modal dialog window? [duplicate]

This question already has answers here:
Modal Dialog without jQuery
(7 answers)
javascript to check when the browser window is closed
(5 answers)
Closed 7 years ago.
When I edit a page and try to leave it I get a dialog box that appears and asks me if I want to:
Leave this Page or Stay on this Page
The dialog blocks me from doing anything until I have answered.
In Javascript the only similar thing I see is window.confirm(message) which gives and OK and Cancel button.
So is there another way in Javascript that I can create a dialog that I don't know about? Please note that I want to create a dialog at any time and I am not interested in knowing when the page closes so this is not a duplicate of the question identified by Zeta.
What happens here, is not an alert() or confirm() or prompt() of any sort.
Write a function for the window.onbeforeunload event handler, load up the parameter with a function passing a message...
➪ ➪ ➪ stackoverflow.com/a/1119324/444255
What it generates is a special native dialog by your browser. (You can´t inspect it with devTools, it's not made from HTML... ok, alerts are neither.)
You can't get the same dialog for other purposes. (If it was possible, the advertising industry would habe let us known long ago ;-)
You can only 'block' either with ugly alert's and it's siblings, or by putting a shim over your page (to prevent clicks) and a html-made dialog on top. Which should stop other interactions with your page, but of course not navigating away.
JavaScript doesn't have a built in modal, only alert, confirm and prompt. Many libraries have made their own modals, Bootstrap and jQueryUI for example.
Your question isn't quite clear. What I understand is that you're interested in the available models.
window.alert() gives you the chance to send a notification to the user.
window.confirm() gives a popup with an Ok and a Cancel button.
window.prompt() gives a popup in which the user can insert text.
http://www.w3schools.com/js/js_popup.asp

detect closing current tab on browser and call ajax function [duplicate]

This question already has answers here:
JavaScript, browsers, window close - send an AJAX request or run a script on window closing
(9 answers)
Closed 6 years ago.
I am working on a website, where at a time one admin can login. I implemented this by saving value in database. Now the problem is when current admin forget to logout, the value in database do not change and another admin can't login again. Even this current admin can't login later on, cause the value in database is checked in condition.
I have looked in window.onbeforeload and window.onunload functions, but it only trigger on page refresh etc, not detecting browser close tab/window. I want to detect closing browser tab and call function upon close, so I can change value in database using ajax.
Any help will be appreciated! Thanks
The event you are looking for is onbeforeunload
Use window.uneforeunload which is trigged when tab/window is closed.
Updated
window.onbeforeunload = function (){
// update your database from here before page closed
}
DEMO

How to trigger jQuery when the browser is closed [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
javascript detect browser close tab/close browser
Does anyone know a reliable way to listen out for a window closing event in javascript/ jQuery?
The window is the parent and not any child instances. I.e. if a window is closed by mistake and the visitor launches their browser again and loads the url previously visited once more.
You can use the window.unload event to set a cookie or use local storage to save the time using new.date(), then see if the visitor returned within a set amount of time.
Something like:
$(window).unload(function() {
localStorage.setItem(“theyLeft”, new Date());
}
then on load check for :
$(window).load(function() {
var timeGoneBy = new Date() - localStorage.getItem(“theyLeft”);
//calculate time gone by, and do something if visitor returned within given time etc.
}
Would need to be refined a lot, and local storage should have cookies as fallback, but just to show the jist of it.
Try the unload method.
The unload event is sent to the window element when the user navigates
away from the page. This could mean one of many things. The user could
have clicked on a link to leave the page, or typed in a new URL in the
address bar. The forward and back buttons will trigger the event.
Closing the browser window will cause the event to be triggered. Even
a page reload will first create an unload event.
You can also try playing with the JS onunload and onbeforeunload events.

How to alert user to not close the window while he has submitted the form to process? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript To Get An Alert When Closing The Browser Window
I am having a site on which users are visiting and uploading video, in that i am having a problem that users are closing the browsers or navigating from the page without letting the upload process to complete, i want to show a alert box to the user to not close the window or not to navigate from the page when uploading process is going on, i tried it through window.unload but it will not work for me because as the uploading process is going on the page will be submitted so that event will always be called, please tell me the way to show the alert box to users on window close or they navigate from the page.
Thanks in advance,
Ravinder Singh
Did you try to use:
window.onbeforeunload

Categories