How to Open URL in New Tab using JavaScript?

In this code snippet, we will see how we can open the website URL in a new tab of the browser using javascript.

Generally, we add the target attribute with value _blank to <a> HTML tag when we are making any link on the website.

The target attribute tells to browser to open this link in a new tab when the user clicks on it. See the following HTML <a> tag reference.

But the same thing we can also do with javascript using the window object. You have to simply use the open() function of window object passing the link and attribute values.

Suppose when you are making some functionality in javascript and you want to open a URL new tab or window based on functionality.

So simply pass the first parameter as the link “https://yourblogcoach.com” and the second parameter is the attribute value “_blank”. See the following code.

Leave a Reply

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