Sometimes, we want to make a link open multiple pages when clicked with JavaScript.
In this article, we’ll look at how to make a link open multiple pages when clicked with JavaScript.
How to make a link open multiple pages when clicked with JavaScript?
To make a link open multiple pages when clicked with JavaScript, we call window.open
.
For instance, we write
<a href="#" class="yourlink">Click Here</a>
to add a link.
Then we write
const a = document.querySelector(".yourlink");
a.onclick = (e) => {
e.preventDefault();
window.open("http://yoururl1.com");
window.open("http://yoururl2.com");
};
to select the link with querySelector
.
Then we set its onclick
property to a function that calls window.open
to open the URL that’s called with.
We call preventDefault
to stop the default link behavior of opening the href attribute URL value.
Conclusion
To make a link open multiple pages when clicked with JavaScript, we call window.open
.