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 :


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="">
     I am an anchor tag.
  2. Now, select the anchor <a> element using the querySelector() method and add the click() method.
    Try it Yourself »
To open the link in a new tab, use the target attribute with its value to _blank. e.g. target="_blank"