开发者

How to get an object of the window which is opened by clicking the link?

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">Op开发者_Python百科en 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)?


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 :)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜