Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

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 check if later if the window was closed.

Hope you guys can help.

like image 288
Giancarlo Corzo Avatar asked Nov 06 '22 05:11

Giancarlo Corzo


1 Answers

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"]);
like image 153
alex Avatar answered Nov 09 '22 03:11

alex