0

I have a scenario where if the user tries to close the current browser window then he should be displayed a confirm box. If he confirms to close the window then this window should close and opening the new window with different url.

I tried use the following code by which I'm able to show the confirm box but I'm unable to figure out how to open the new window if user confirms to close the current.

var preventUnloadPrompt;            
$('a').live('click', function () { preventUnloadPrompt = true; });
$('form').live('submit', function () { preventUnloadPrompt = true; });
$(window).bind("beforeunload", function () {
    if (preventUnloadPrompt)
    { return; }
    else {
        return confirm("quit??");            
    }
});

Any type of help is appreciated. Thanks.

Nishant
  • 7,504
  • 1
  • 21
  • 34
  • In theory it should be possible using `window.open`, but it isn't a good idea as most browsers will likely prevent calling `window.open` in the `unload` event. What do you need to do this for? Surely if a user tries to close the tab then they want to leave the site, not open a new tab...? – ClarkeyBoy Jul 28 '12 at 15:54
  • Actually there are set of questions being asked one by one to a user. If user closes this window I want to execute code on some different page which would mark, that this user has attempted the set of questions and hence prevent him to re-attempt the question again. Normally this happens when the user finish attempting all the questions. – Nishant Jul 28 '12 at 16:00
  • Now I know who's coding those annoying web-sites that are **impossible to close**! It's my computer, it's my browser, Damn it I should be free to close it whenever I'm pleased! – YePhIcK Jul 28 '12 at 16:01
  • What kind of websites do you go on, @YePhIck? :P – ClarkeyBoy Jul 28 '12 at 16:01
  • On a serious note - aside from the fact that this design is annoying as hell it's also something you can't reliably do in modern browsers (which is a good thing) – YePhIcK Jul 28 '12 at 16:03
  • 2
    @NishantSaini: What I would do is to use a flag against the userid and questionid which is set to 1 on page load. On page load you check if this flag is set to 1 to determine whether or not to display the question. There may also be a need for a random number against the userid / questionid too which is submitted with the form data, so when they submit it is detected as being the first view of the question and is therefore valid. Does this help? – ClarkeyBoy Jul 28 '12 at 16:05
  • @YePhIcK I don't want something that is impossible to close. I just want the process to complete properly. :) – Nishant Jul 28 '12 at 16:20
  • @YePhIcK: I would not have asked this question if I would have known how to do that :P ;) And yeah this design is very very annoying; but I'm in a situation that I don't have time to redesign it. – Nishant Jul 28 '12 at 16:22
  • 1
    Use the solution proposed by @ClarkeyBoy, it is a pro way. The browser belongs to the user and user is in control. The server is the reign for developer. Your business constraints should not depend on client side, it is not reliable. – Marcelo De Zen Jul 28 '12 at 16:34

2 Answers2

1

When a window closes, everything, absolutely everything associated with that window whether visible or not ... pending javascript code, stuff on screen, etc. vanishes and that includes the very scripts that are in the middle of execution - poof - gone nada, basically canceling, aborting, halting, stopping any running script.

The temporal sequencing requires that, except for closing a window, all other user initiated operations be completed first.

example:

javascript:
void(window.open("data:text/html,
<html>
<a href=\"javascript:window.open('about:blank');self.close();void(0);\">open & close</a> this link does both<br/>
<a href=\"javascript:self.close();window.open('about:blank');void(0);\">close & open</a> this does close but open fails
</html>
"))

The links can be preconditioned with if(confirm("quit??")){ ... }

tested with:

window.navigator.userAgent =
Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0) Gecko/20100101 Firefox/11.0

Bookmark:
Open a new browser window on close of current window if user confirms to close current window

Community
  • 1
  • 1
ekim
  • 118
  • 1
  • 2
-1
var answer = confirm("quit??");
if (answer){
    window.open("http://www.google.com/");
}
else{
// do something else
}
Adrian Bratu
  • 498
  • 2
  • 9
  • An "answer" is supposed to be something you *recommend* as a working solution. For everything else there's a "comment" section where you can ask more questions or try things out – YePhIcK Jul 28 '12 at 16:05
  • No it doesn't. Anyways thanks for the reply. I think confirm doesn't work with beforeunload. What I noticed is that return confirm("Quit??"); is not right. Its not the one that results in confirm box. If I write return "Quit??" thn the confirm box shows Quit?? in first line and then it has string saying Are you sure you want to leave this page? and two buttons Stay on this page and Leave this page. – Nishant Jul 28 '12 at 16:18
  • confirm() is blocked on "beforeunload" event in chrome. Firefox will display it, but ignores the return and the event.preventDefault(). In other words, it is impossible not to close the window if user want it closed. – Marcelo De Zen Jul 28 '12 at 16:35