I want to open a popup when the user closes the last tab of the current site. The problems I have is that
- when the browser prompts the user (
onbeforeunload
returning a non null value), then the popup is blocked by the browser (see case 1 below) - when the browser does not prompt anything to the user (
onbeforeunload
not returning anything), then the popup is not opened at all and seemed to be totally ignored by the browser (why is that?) (see case 2 below)
Case 1: prompt and popup blocked (code from this answer)
window.onbeforeunload = function (e) {
e = e || window.event;
window.open('http://localhost', '_blank');
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Sure?';
}
// For Safari
return 'Sure?';
};
Case 2: no prompt, no popup, page closing "normally"
window.onbeforeunload = function (e) {
e = e || window.event;
window.open('http://localhost', '_blank');
};