JavaScript

How to open a URL in a new tab in Javascript

In this tutorial, we are going to see how to open a URL in a new tab in Javascript. The target attribute of the <a> tag is an easy way to open a URL in a new window or in a new browser tab. But sometimes you have to open a URL with JavaScript. The JavaScript window.open() method opens a new browser window.
 

Use the target attribute to open a new tab

We can use <a> to open a new tab by setting the [target = "_blank"] attribute.

<a href="https://stackhowto.com" target="_blank">StackHowTo</a>

This will open the link in a new tab.
 

Using window.open()

We can achieve the same goal by using window and its open() method via javascript.

window.open('https://stackhowto.com', '_blank');

This will open the URL in a new tab.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *