2 Ways to Open URL in New Tab Using JavaScript

Bikash Panda
Oct 17, 2020

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,

https://phpcoder.tech/open-url-in-new-tab-using-javascript/

<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,

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

Also Check:

Happy Coding..!

--

--

Bikash Panda

I am a web application developer. FB Community: @mrpandatech, Twitter: @phpcodertech My PHP problem-solving site http://phpcoder.tech