开发者

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

开发者 https://www.devze.com 2023-01-28 11:20 出处:网络
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.

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

精彩评论

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