How to get a link to an open window with a target blank

I am trying to get a link to the javascript window that opens after clicking the ANCHOR button with the target _blank.

Like this:

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

Therefore, I can check whether later if the window was closed.

Hope you guys can help.

+3
source share
2 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;
}

Take a look!

If you just need all the links with target="_blank", this should succeed in choosing 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]);
    }

}

In the new browser ...

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

Why should you set a goal in _blank? Can't you just give that name?

0
source

Source: https://habr.com/ru/post/1778212/


All Articles