Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript open new tab, and wait for it to close

Tags:

javascript

I want to open a new tab for a bank payment on my website using javascript, and without the main window navigating away,

when the user is back from the bank payment to a return URL, i want to detect the reply of the return URL in from the other window ( if possible ) or just notify the main window that the transaction is done and it should check the database for updates.

I've seen this behaviour on several websites, such as popup->login->popup closes->main window reloads with the loaded session, The problem is that i don't know what this method is called, so I don't know what keyword I'm looking for.

What I exactly need is either the name of this method, or how is it done ( as a certain keyword in javascript or something )

Thanks in advance

like image 994
Mohammad AbuShady Avatar asked Jun 18 '13 14:06

Mohammad AbuShady


2 Answers

Before closing the popup, try opener.someFunction() to call a function in the window that opened it. Note that this will fail if the user closed the first window, or if the user navigated it to another site, or if the two windows are on different domains for whatever reason.

like image 127
Niet the Dark Absol Avatar answered Oct 09 '22 21:10

Niet the Dark Absol


You can open a new page using window.open() and then check periodically to see if the window has been closed. The reference to the opened window is returned by window.open() and you can check if it has been closed using windowHandle.closed.

btn.onclick = () => {
  const win = window.open(
      'https://www.stackoverflow.com/',
      'Secure Payment');
  const timer = setInterval(() => {
    if (win.closed) {
      clearInterval(timer);
      alert('"Secure Payment" window closed!');
    }
  }, 500);
}

See, also, this short demo.

For more info on window.open() and best practices, take a look at MDN.

like image 27
gkalpak Avatar answered Oct 09 '22 22:10

gkalpak