Onunload doesn't work in JS - javascript

window.onunload = function()
{
confirm("close the window?")
}
Why don't I have the confirm window coming out when closing a window?

Presumably it is to prevent pages from repeatedly preventing you from closing a page. With the beforeunload event, you can, however, get what is, in most modern browsers, a non-customizable or only partly customizable dialog prompting the user whether to leave or not.

Many browsers not support window.unload.
Please view this link.
window.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera?

Most browsers prevent the use of alert/confirm in onunload.
Instead use an onbeforeunload handler that returns a string:
window.onbeforeunload = function() {
return "Don't go!";
};
This will then be presented to the user in a dialogue box.
See also: window.unload() won't work in jQuery

Related

onunload not working in combination with onbeforeunload in Safari

I want to make sure the user gets a warning when closing the window on some occasions (unsaved data, for example). Works well.
BUT: I also need to do some clean up work BEFORE the window closes but AFTER I asked whether the user really wants to close it.
So I have two callback functions, one for onbeforeunload that will throw the confirmation dialog, and one for onunload that should do the cleanup.
Unfortunately, in Safari, the onunload callback does not seem to be called.
Why?
$(window).on('beforeunload',function() {
if (connected)
return "Are you sure you want to close the window!";
});
$(window).on('unload',function() {
localStorage.removeItem("someItem"); // never executed in Safari
});
Unfortunately, this event is not supported in some of the browsers. You can try using pagehide event instead of onunload.
Good Luck!

Disallow JavaScript from closing window in IE

I'm trying to debug an issue where the browser disappears after refreshing a particular page. I want to see if I can eliminate JavaScript as being the cause of this issue.
Does IE have an equivalent to the Firefox configuration option dom.allow_scripts_to_close_windows?
I did some googling and this was the closest thing I could find, but I do not know if it also prevents JavaScript from closing windows.
Prevent Users from Closing the Browser or Explorer Windows
Edit: You can use the onbeforeunload event to see if the page is triggering some sort of close event.
window.onbeforeunload = function() {
return "The browser is about to close this window, are you sure?"; // Will appear as a confirmation box
};

Difference between onbeforeunload and onunload

What are the differences between onbeforeunload and onunload ?
Also I have a specific question related to it's use on the iPad...I have a page (myPage.html) where I am trying to show an alert when the page is closed (i.e. X is pressed to close the tab on iPad)
Now I tried using both window.onunload and window.onbeforeunload
Below are my findings on the iPad;
Using window.onunload , I am able to get an alert when user navigates to a different page from myPage.html (either by clicking on some link or doing a Google search while on myPage.html) . However nothing happens when the tab is closed from the minimized view (X)
Using window.onbeforeunload, I neither get an alert even if the user navigates to a different page from myPage.html OR if he closes the tab (X) from the minimized view.
I wanted to know if there is any alternate way to fix this issue ?
Thank you.
onunload is responsible for executing an instruction when the page is closed. It also causes issue with IE and AJAX.
onbeforeunload is more efficient because it does not run in competition with the actual closing of the window and is triggered before onunload
I know Opera used to not acknowledge onbeforeunload - not sure if they've fixed that, but I always register the listener for both to be safe:
window.onunload = window.onbeforeunload = (function(){...
Adding with AlienWebguy's ans, to avoid dual calls at the browsers that support both events,
var onBeforeUnLoadEvent = false;
window.onunload = window.onbeforeunload= function(){
if(!onBeforeUnLoadEvent){
onBeforeUnLoadEvent = true;
//your code here
}
};
onbeforeunload:
Called before unloading begins
MDN tells me you can cancel the closing of the page using event.preventDefault();
or by returning a non-void value (ie a value != 0), the page will pop up a confirmation dialog that allows the user to choose to cancel the close
MDN also says Opera 12 and up support onbeforeunload - looks like its supported it for a while now
onunload:
Called after unloading has begun, but before any resource deallocation (its not clear to me what exactly is done during this period)
Its too late to cancel the page close at this point
The only reason I can think of why you would want to use onunload over onbeforeunload would be where your onunload code could take some significant time, and don't want the user to see a window that hangs while closing.
One significant difference (other than cancelability) between the onbeforeunload and onunload is that the former is triggered for download links and the latter is not. Example: download will trigger the onbeforeunload handler, but not the onunload.

What user actions trigger beforeunload in which browsers?

closing window
refreshing page
clicking on a link
??
It seems to be inconsistent. (I'm shocked!) Is there a list somewhere? I'm particularly interested in Firefox, but am curious about others, as well. The Mozilla docs are vague:
window.onbeforeunload
An event that fires before the unload event when the page is unloaded.
window.onunload
The unload event is raised when the document is unloaded.
Gee, thanks.
window.onbeforeunload will trigger before you navigate to away from current page (from URL address, BACK button, close browser, etc.)
The event will not fire in Opera. But all the other browsers seem to respect it OK.
I have used this in the past for AJAX-intensive sites, mostly as a way to avoid having to support the BACK button.
It also works well as a confirmation dialog to prevent you from accidentally losing your work. But users might get annoyed by the confirmation. So this might not be a good enough reason to use it.

Open new window after a click event not working in Safari, Chrome

I'm trying to open a new window like so:
$('#wrapper').click(function() {
window.setTimeout(function() {
//alert('hi');
window.open("http://example.com", "ExternalLinks", "resizable=yes, scrollbars=yes, status=yes");
}, 1000);
});
This works in Firefox, but not in Chrome or Safari (so far, I've just tested on a Mac). The alert() works in all browsers, so there seems to be something preventing the window.open from executing in Safari/Chrome. Furthermore, if I remove the setTimeout and just call the window.open then it does work in all 3 browsers. It's almost like if the window.open is nested too far away from the click event, then it doesn't work in Safari/Chrome.
So you know, I have an all-Flash website and I'm trying to get external links to open in a new window, so I'm reading the hash tag in the URL (ex. htp://example.com/#/facebook/) and if it matches certain items, then I'm calling window.open to open a specific URL. I don't have access to the Flash source, or I would handle this there.
Any ideas?
Safari/Chrome have built-in pop-up blockers that stop this from working. The only javascript that is allowed to open a new window in Safari/Chrome is javascript directly attached to click handlers (and other direct user input handlers). In past versions people figured out some ways to cheat (like generating some other element -- a form or div -- and simulating user input with javascript), but newer versions are smarter about detecting this. I'd recommend re-configuring things so that you don't use a delayed pop-up -- that is the kind of thing that can generally be jarring to a user after all.
I got around this by checking the return value of window.open() for undefined. If that is true call alert() with a message for the user to to disable their popup blocker.
var myWin = window.open([args]);
if (myWin == undefined)
alert('Please disable your popup blocker');
Another workaround
Just open a popup with ACCEPT and CANCEL options and attach the window.open action to the ACCEPT button and it will works. It worked for me...

Categories