JavaScript - open link in new tab

5 points
Created by:

In this short article we would like to show how in JavaScript open some link in new tab.

Quick solution:

// ONLINE-RUNNER:browser;'', '_blank');


  • many browsers blocks popupping windows this way -┬ásometimes it is better to┬áconsider simple link in html code with target="_blank" attribute,
  • to avoid browser blocking effect it is good to run above action inside mouse click event┬áfunction - check last example.

Read next sections for more details.

Link with click symulation example

In this approach it is necessary to create new link element and simulate clicking on it.

// ONLINE-RUNNER:browser;

var a = document.createElement('a');

a.setAttribute('href', '');
a.setAttribute('target', '_blank');;

Avoiding blocking by web browser example

In this section, open in new window action is run during onclick event to prevent blocking operation.

// ONLINE-RUNNER:browser;

<!doctype html>
  <span onclick="'', '_blank')">
    Click me and open new tab


Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé