Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open and then close a window without getting blocked as a popup?

I want to direct a customer in an e-commerce site to pay via Paypal's website. I would like the payment to be done in a new tab/window so the customer doesn't lose the current state of the web page he/she is at.

In order for the Paypal window to open without getting blocked, I am using an anchor with target="_blank". Which is working perfectly except for the fact, I can't close it after Paypal payment is done since window.close() doesn't work for windows that were not opened via window.open().

How do I make it so it is BOTH not blocked as a popup AND I am able to close it with JS later on?

like image 511
Yuval Cohen Avatar asked May 22 '12 12:05

Yuval Cohen


People also ask

How do I make my pop-up window not blocked?

Chrome® on AndroidTap the three dots in the upper right-hand corner and select Settings. Scroll down to the Advanced settings and select Site settings. Select Pop-ups and redirects. Toggle the switch on or off depending on your preferences.

How do I bypass a pop-up blocker?

To disable the pop-up blocker, uncheck the Blocked (recommended) box. To enable pop-ups on specific sites, check Blocked (recommended) and click Add next to Allow and enter the URL(s). CHROME (MAC) By default, Chrome will notify you when a pop-up is blocked and give you an option to see it.

What triggers pop-up blocker?

Most popup blockers are triggered when a popup is launched indirectly from a user action. Some popup blockers are triggered when a user clicks, but most are not. Basically, if the popup is triggered within a click handler (or code that it calls), you are generally okay.


1 Answers

In order for the Paypal window to open without getting blocked, I am using an anchor with target="_blank".

That's one option, but as long as you call window.open from within the handler for a user-generated event (like click), you can open pop-up windows. So just make sure you call window.open from within a click handler on the link (and then you can close it). Modern pop-up blockers (anything from the last several years) block pop-ups that aren't triggered by a user event, but allow ones that are.

Live example | source:

HTML:

<p><a href="#" id="target">Click to open popup</a>; it will close automatically after five seconds.</p>

JavaScript:

(function() {

  document.getElementById("target").onclick = function() {
    var wnd = window.open("http://stackoverflow.com");
    setTimeout(function() {
      wnd.close();
    }, 5000);
    return false;
  };

})();
like image 129
T.J. Crowder Avatar answered Oct 05 '22 12:10

T.J. Crowder