1

What I want to do is detecting the browser close event (on browser unload) and open up a new pop up where we have some options like (give a feedback, go to the page again , mail me a report etc). At the same time I want the parent window alive till the user select an option in pop up window. I tried following and it seems not working.

$(window).bind('beforeunload', function(event){
        event.preventDefault();
    });

Please give me the right direction for achieving this.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Eshan Sudharaka
  • 607
  • 2
  • 9
  • 16
  • 1
    I can simply negate this 'feature' by closing the browser window. Why do you want to annoy your visitors like this? –  Aug 08 '13 at 04:35
  • This question may assist you: http://stackoverflow.com/questions/3256919/displaying-a-custom-dialog-when-the-user-exits-the-browser – Shaun Aug 08 '13 at 04:35

2 Answers2

2

you can only do it with an alert/dialog

$(window).bind('beforeunload', function(event){
    return "hold on a minute there, conme back and answer some questions";
});
borbulon
  • 1,391
  • 1
  • 11
  • 11
  • this will pop up default browser exit popup with different text. You mean firing the customized window if the user click yes – Eshan Sudharaka Aug 08 '13 at 04:41
  • 1
    Right, but the only thing to stop the page from unloading is the dialog. That's it. Extra windows don't stop the page from unloading. I think the bigger question is why you would load a new window when a user has already decided to leave. It's really, _really_ bad UX. You're better off with a `return "Before you leave, would you mind answering a few questions for us? We'd love to have your feedback.";`, and then popping up some sort of modal. Even that is pretty bad UX, but it's not nearly as bad as "hey, we know you've made the decision to leave, but here's another window from us instead" – borbulon Aug 08 '13 at 14:03
0

You can do this, but users won't see it in Chrome unless they disable the pop-up blocker.

$(window).on("beforeunload", function(){
    window.open("survey.html");
    return "Are you sure you want to leave?";
});
Brigand
  • 84,529
  • 20
  • 165
  • 173