Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu September 04, 2022 . #anchor . #HowTo

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.

  1. 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>
  2. 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
Write Your Answer
loading
back
View All