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.
<a href="..." target="_blank" id="my-link">
new window
</a>
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"]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With