How to simulate target=“_blank” in JavaScript ? Last Updated : 02 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In web development, the target="_blank" attribute on anchor (<a>) tags is used to open links in a new browser tab or window. Sometimes, developers might need to achieve this behavior programmatically using JavaScript. Understanding these techniques equips developers with flexible solutions for enhancing user experience and managing navigation behavior seamlessly in web applications.HTML Target AttributesThe HTML target attribute defines where the linked document will open when the user clicks on the link.If target="_blank" is set on an anchor element, the linked document will open in a new tab; otherwise, the document will open in the same tab.Syntax document.addEventListener("click", function(e) { if (e.target.tagName == "A" && !e.target.hasAttribute("target")) { e.target.setAttribute("target", "_blank"); }}); Example: To demonstrate simulating target with the _blank value using JavaScript. 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, the target attribute is "_blank". html <!DOCTYPE html> <html> <head> <title> Simulate target=“_blank” in JavaScript </title> <script> document.addEventListener("click", function (e) { if (e.target.tagName == "A" && !e.target.hasAttribute("target")) { e.target.setAttribute("target", "_blank"); } }); </script> </head> <body style="text-align:center"> <a href="https://www.geeksforgeeks.org/"> GeeksforGeeks<br> A computer science portal for geeks </a> </body> </html> Output: Comment More infoAdvertise with us Next Article How to navigate URL in an iframe with JavaScript ? A ankitabag123 Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2019 JavaScript-Misc JavaScript-Questions +2 More Similar Reads How to Simulate a Click in JavaScript? Simulating a click in JavaScript means triggering a click event on an HTML element using code, rather than requiring manual user interaction. This technique is useful for automating actions like clicking buttons, links, or other interactive elements, enhancing web functionality, and triggering hidde 3 min read How to redirect to a relative URL in JavaScript? To redirect to a relative URL in JavaScript, you could use window.location.href. It is a property in JavaScript that represents the complete URL of the current page. It can be used to get the current URL or to navigate to a new URL by assigning a new URL to it. Approach HTML Structure:The HTML file 2 min read How to navigate URL in an iframe with JavaScript ? To navigate the URL in an iframe with JavaScript, we have to set the src attribute or return the value of the src attribute in an iframe element. The src attribute defines the URL of the document that can be shown in an iframe.Syntax:document.getElementById("selector").src = "URL";URL values: Absolu 1 min read How to Get Browser to Navigate URL in JavaScript? As a web developer, you may need to navigate to a specific URL from your JavaScript code. This can be done by accessing the browser's window object and using one of the available methods for changing the current URL.In JavaScript, there are several approaches for navigating to a URL. The most common 4 min read How to Create a Link in JavaScript ? In JavaScript, a link typically refers to creating HTML hyperlinks (<a> tags) dynamically using JavaScript. You can link using a document.createElement('a'), setting attributes like href and textContent, and then appending it to the document with appendChild().Here are some common approaches t 4 min read How can a page be forced to load another page in JavaScript ? In JavaScript, you can force a page to load another page by using the window.location object. There are a few methods to achieve this. To force a page to load another page in JavaScript, we have multiple approaches: Below are the approaches used to force a page to load another page in JavaScript: Ta 2 min read Like