There are a couple of ways to open a URL in a new tab using JavaScript.

The first way is to use the window.open() method. This method opens a new window and can be used to open a URL in a new tab. Here is an example:

window.open("https://www.example.com", "_blank");

Another way is to use the <a> tag with target=”_blank” attribute. You can create an anchor tag and set its href to the URL you want to open and target attribute to “_blank”.

let a = document.createElement('a');
a.href = "https://www.example.com";
a.target = "_blank";
a.click();

This will open the URL in a new tab.

It is worth noting that both of these methods will open a new tab if the user has not turned off that feature in their browser settings.

Also Read:

Categorized in: