Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to simulate target=“_blank” in JavaScript ?

  • Difficulty Level : Medium
  • Last Updated : 17 Feb, 2020

The HTML target attribute defines where the linked document will open when the user clicked on the link. If target=”_blank” is set with anchor element, then the linked document will open in a new tab otherwise document is open in the same tab. There are two methods to execute this task. One is the normal older and lengthy way, where target=”_blank” attribute is written inside the HTML tag. Another convenient way is to execute through javascript code.

Approach: At first we will create an event on every click function then we set the condition if there is an anchor tag and the target attribute is not mentioned, target attribute as “_blank”.


 document.addEventListener("click", function(e) {
    if ( == "A" &&
    {"target", "_blank");

Below example illustrate the target=”_blank” in JavaScript:

<!DOCTYPE html>
        Simulate target=“_blank” in JavaScript
        document.addEventListener("click", function(e) {
            if ( == "A" &&
      "target", "_blank");
<body style="text-align:center">
        A computer science portal for geeks


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!