How To

Open URL in New Tab Using JavaScript

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

Written by Bikash · 1 min read >

In HTML we use an anchor tag <a> for a hyperlink to another page. For Open URL in New Tab Using JavaScript, we use the anchor tag target attribute to open in a new tab using JavaScript.

In HTML anchor <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,

Open URL in New Tab Using JavaScript

If you want to do the with javascript to open a 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

To know more about window.open() method check here: https://developer.mozilla.org/en-US/docs/Web/API/Window/open

Syntax:

Example of Open URL in New Tab By Using JavaScript

Below example shows provide URL open in new tab using JS.

Code Highlights:

  • In the above example, we create an HTML button where we call a JS function on onclick event to open in a new tab using JS code.
  • Now we create a JS script, where we use the window open method to open URL in a new tab by JavaScript.
  • Window open method takes two parameters,
    1. URL: which opens in new tab JS.
    2. To open in a new tab we mainly use _blank attribute as the second parameter.

Here is the complete source code and explanation of open URL in new tab using JS.

Also Check:

Tags: #javascript #window.open() #JavaScriptwindow.openNewTtab

Happy Coding..!

Was this article helpful?
YesNo
Written by Bikash
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP. Profile