2 Ways to Open URL in New Tab Using JavaScript

In HTML we use <a> tag for a hyperlink to another page. For Open URL in New Tab Using JavaScript we use <a> tag target attribute.

In HTML <a> tag’s target attribute provides a simple way to open a new tab or new window of the browser. We use _blank as a value in the target attribute in the anchor tag. Check below,


<a href=”http://www.phpcoder.tech" target=”_blank”>Visit PHPCODER.TECH</a>

If you want to do the with javascript to open link in a new tab, then we can use the window.open() the method as the best option in javascript.

window.open() method take two parameters,

  1. URL
  2. _blank value

Syntax of Open URL in New Tab using JavaScript

window.open(“URL”, “_blank”);

Below example is open http://www.phpcoder.tech URL in a new tab of the browser,

<p>Click the button to open a new tab </p>
<button onclick=”NewTabJS()”>
function NewTabJS() {
window.open(“http://www.phpcoder.tech", “_blank”);

Also Check:

Happy Coding..!