Home > Tips & Tricks > How to Open URL in New Tab using JavaScript?

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.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment