If you want to open a link in a new tab using JavaScript, you can use the window.open()
method. Here's an example:
const link = "https://www.example.com";
const win = window.open(link, "_blank");
win.focus();
The window.open()
method opens a new browser window or tab, depending on the user's browser settings.
"_blank"
value specifies that the link should be opened in a new tab or window.Finally, the win.focus()
method sets the focus to the newly opened window or tab.
Here is an online example on JSFiddle:
Note that in HTML (without JavaScript), you can open a link in a new tab by setting the target
attribute of an <a>
tag to "_blank"
. Here is an example:
<a href="https://www.example.com" target="_blank">Open in new tab</a>