8

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?

Jamie F
  • 23,189
  • 5
  • 61
  • 77
Yuval Cohen
  • 185
  • 1
  • 1
  • 6
  • http://stackoverflow.com/questions/888964/javascript-window-open-is-blocked-by-ie-popup-blocker and http://stackoverflow.com/questions/9616150/window-open-getting-blocked-in-some-browsers possible duplicate – shareef May 22 '12 at 12:49
  • Put the state information in the callback URL. Don't create a multiple window workflow. – Quentin May 22 '12 at 12:52
  • http://stackoverflow.com/questions/7139103/open-page-in-new-window-without-popup-blocking lack of research.. possibly duplicate question – yajay Jul 02 '13 at 10:18

1 Answers1

27

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;
  };

})();
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875