views:

25

answers:

2

Hi

I've got a link on the web page that causes new browser window (tab) to be opened when it's clicked, like this:

<a id="lnkNewWindow" target="_blank" href="http://google.com"&gt;Open window</a>

I want to be able to track the window that will be created after this link is clicked. I'd like to perform some actions after the new window is closed. Is there any way to do this (preferably using jQuery)?

A: 

You can grab the handle directly if you use the window.open() method. This is an old-school method, but still works like a charm. Take a look here: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml

shinkou
That's pretty clear, but is it possible to do this somehow handling click event of that link with jQuery?
the_V
+1  A: 

You can get it using by window.open() for the new window, like this:

$("#lnkNewWindow").click(function() {
  var win = window.open(this.href);
  //do stuff with win, e.g. win.onload
  return false; //prevent normal link behavior
});

You can also now remove the target attribute from the anchor, it's no longer needed...and you're XHTML valid to boot, if that mattered at all :)

Nick Craver
I really need link to be opened in the new window(tab). I've just tried to do what you advised and new tab wasn't created. I was redirected to the proper URL, but stayed at the same tab.
the_V
Oh, I've figured it out. Thanks for your assistance!
the_V