Javascript programming articles and tutorials for beginner | Anjan Dutta

How to open URL in a new tab using javascript

How to open URL in a new tab using javascript

To open a URL in a new tab, we can use the window.open() function.

The code looks like below.

var URL = 'https://anjandutta.com';
window.open(URL, '_blank');

In the above example, we are passing two parameters.

If we pass more than two parameters, then the URL will open in a new window rather than a tab.

The first parameter is the URL we are willing to load and the second parameter is the target parameter.

In place of the second parameter, we can pass several other values like below.

  • _blank - the URL is loaded into a new window or a tab.
  • _parent - the URL is loaded into the parent frame.
  • _self - the URL replaces the current page.
  • _top - the URL replaces any framesets that may be loaded.
Edit this page on GitHub