-1

i have a strange query, i want to open a new window (a popup), when the browser/tab is closed using asp.net, Jquery, but i want to bypass the popup blocker which blocks the window, can anyone help me with this, how can i open a popup when user closes the browser/tab, or any other alternatives which can help me achieve the same. the main problem is i want to ignore the popup blocker. on one the SO Post

i read the below example could help:

jQuery(function($) {
  // This version does work, because the window.open is
  // during the event processing. But it uses a synchronous
  // ajax call, locking up the browser UI while the call is
  // in progress.
  $("#theButton").click(function(e) {
    e.preventDefault();
    $.ajax({
      url:      "http://jsbin.com/uriyip",
      async:    false,
      dataType: "json",
      success:  function() {
        window.open("http://jsbin.com/ubiqev");
      }
    });
  });
});

i replaced the click event with $(window).unload but that too didnt helped. the popup does not opens, but when i remove e.preventDefault(); the popup opens, but require the popup-blocker be enabled.

Community
  • 1
  • 1
Abbas
  • 4,948
  • 31
  • 95
  • 161

3 Answers3

3

I don't think there's a way to go around pop-up blockers.

You should change the approach and try to open the content in a jQuery UI modal dialog box, instead of using an actual browser window popup.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • ok as per going with jQuery UI modal dialog box, can i open the modal dialog on close of browser/tab. – Abbas Sep 27 '12 at 16:45
  • 1
    jQuery's modal dialog is an HTML emulation of a modal popup, thus, it has to be inside a page. You could open it before the closing of the window – Adriano Carneiro Sep 27 '12 at 16:47
  • can you help me how shall i open the modal popup before closing window, i am using onbeforeunload, but here the problem is when i close the window, it first displays alert message and if user clicks stay on page, only then the modal is visible, and also the leave message box displays on page load too – Abbas Sep 27 '12 at 19:48
1

You have to open the window inside the same function as the $.ajax, otherwise some browsers will still reject the popup.

jQuery(function($) {
  // This version does work, because the window.open is
  // during the event processing. But it uses a synchronous
  // ajax call, locking up the browser UI while the call is
  // in progress.
  $("#theButton").click(function(e) {
    // use success flag
    var success = false;
    e.preventDefault();
    $.ajax({
      url:      "http://jsbin.com/uriyip",
      async:    false,
      dataType: "json",
      success:  function() {
          success = true; // set flag to true
      }
    });
    if (success) { // and read the flag here
        window.open("http://jsbin.com/ubiqev");
    }
  });
});

This is the only reliable way to make sure the uriyip gets called AND popup a window when it's done loading; so it freezes the browser, too bad.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

Popup blockers are designed to prevent this behavior.

I would suggest using a modal window instead of an actual browser window. I don't think that these get blocked because they are opened within the page itself.

As for the event... You could do something like...

window.onbeforeunload = function whatever() {
       //Do code here for your modal to show up.
 }

If you are just trying ot give a warning or something you could do

window.onbeforeunload = function showWarning() {
       return 'This is my warning to show';
 }
Cruril
  • 450
  • 4
  • 14