开发者

How to get the reference of an opened window with target blank

I'm trying to get the reference by javascript of a window that it's open after clicking an ANCHOR that has a target _blank.

Like this:

<a href="..." target="_blank">
new window
</a>

So I can c开发者_StackOverflow中文版heck if later if the window was closed.

Hope you guys can help.


HTML

<a href="..." target="_blank" id="my-link">
new window
</a>

JavaScript

var link = document.getElementById('my-link');

link.onclick = function() {
    var reference = window.open(link.href, '_blank');
    return false;
}

See it!

If you simply wanted all links with target="_blank", this should succeed in selecting them.

var allLinks = document.getElementsByTagName('a'),
    blankLinks = [];

for (var i = 0, linksLength = allLinks.length; i < linksLength; i++) {

    if (allLinks[i].getAttribute('target') === '_blank') {
        blankLinks.push(allLinks[i]);
    }

}

On a fresh browser...

var blankLinks = document.querySelectorAll('a[target="_blank"]);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜