Reputation: 183
The following code resembles a click on all links in the page (of course, after the first one the page will change and all task won't be completed and I give it just as an example):
document.querySelectorAll('a').forEach((e)=>{
e.click();
});
What if I want to open all links in the page in new windows (then the task will be completed).
I tried this in console:
document.querySelectorAll('a').forEach((e)=>{
e.window.open();
});
And
document.querySelectorAll('a').forEach((e)=>{
e.click(window.open());
});
And
document.body.addEventListener(function(e) {
if (e.target.nodeName.toUpperCase() === 'A' && e.target.href) {
e.target.target = '_blank';
}
}, true);
None worked.
What is the right syntax?
Upvotes: 0
Views: 36
Reputation: 785
document.querySelectorAll('a').forEach(function (el, i) {
console.log(el.href, i);
window.open(el.href, 'link' + i);
});
Correct syntax would be the one above however browser will not let you open multiple windows at once due to potential abuses. Imagine a page that opens 20 new tabs for you ... not nice right.
Upvotes: 1