How to open a link on page load using Javascript
Suppose, you have an anchor (<a>) element. You want to open the link in a new tab after loading the page. So that, you don't have to click the link manually.
To click a link after loading the page, you have to use the Javascript's click() method.
Syntex :
document.querySelector('a').click();
To open a link in a new tab, follow the below steps.
- Create an anchor tag with a class name. So that, we can select the anchor tag by the class name.
<a class="my-anchor" href="https://www.example.com"> I am an anchor tag. </a>
- Now, select the anchor <a> element using the querySelector() method and add the click() method.
document.querySelector('.my-anchor').click();
Try it Yourself »
To open the link in a new tab, use the target attribute with its value to _blank. e.g. target="_blank"
save
listen
AI Answer
How to open a link on page load using Javascript
0
Suppose, you have an anchor ( <a> ) element. You want to open the link in a new tab …
asked
Apu
0 answers
2915
Suppose, you have an anchor ( <a> ) element. You want to open the link in a new tab …
Answer Link
answered
Apu