{"id":134115,"date":"2023-06-21T05:10:31","date_gmt":"2023-06-21T05:10:31","guid":{"rendered":"https:\/\/phppot.com\/?p=21003"},"modified":"2023-06-21T05:10:31","modified_gmt":"2023-06-21T05:10:31","slug":"javascript-how-to-open-url-in-new-tab","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/06\/21\/javascript-how-to-open-url-in-new-tab\/","title":{"rendered":"JavaScript \u2013 How to Open URL in New Tab"},"content":{"rendered":"<div id=\"tutorial\" readability=\"36.900394045534\">\n<div class=\"modified-on\" readability=\"7.0697674418605\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on June 25th, 2023.<\/div>\n<p>Web pages contain external links that open URLs in a new tab. For example, Wikipedia articles show links to open the reference sites in a new tab. This is absolutely for beginners.<\/p>\n<p>There are three ways to open a URL in a new tab.<\/p>\n<ol>\n<li>HTML anchor tags with <em>target=_blank<\/em>&nbsp; attribute.<\/li>\n<li>JavaScript <em>window.open()<\/em> to set hyperlink and target.<\/li>\n<li>JavaScript code to create HTML link element.<\/li>\n<\/ol>\n<h2>HTML anchor tags with <em>target=_blank<\/em>&nbsp; attribute<\/h2>\n<p>This is an HTML basic that you are familiar with. I added the HTML with the required attributes since the upcoming JavaScript example works with this base.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-html\">&lt;a href=\"https:\/\/www.phppot.com\" target=\"_blank\"&gt;Go to Phppot&lt;\/a&gt;\n<\/code><\/pre>\n<h2>Scenarios of opening URL via JavaScript.<\/h2>\n<p>When we need to open a URL on an event basis, it has to be done via JavaScript at run time. For example,<\/p>\n<ol>\n<li>Show the PDF in a new tab after clicking generate PDF link. We have already seen <a href=\"https:\/\/phppot.com\/php\/generate-pdf-from-html\/\">how to generate PDFs using JavaScript<\/a>.<\/li>\n<li>Show product page from the gallery via Javascript to keep track of the shopping history.<\/li>\n<\/ol>\n<p>The below two sections have code to learn how to achieve opening URLs in a new tab using JavaScript.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-21249\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/06\/javascript-open-in-new-tab-550x407.jpg\" alt=\"javascript open in new tab\" width=\"550\" height=\"407\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/06\/javascript-open-in-new-tab-550x407.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/06\/javascript-open-in-new-tab-300x222.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/06\/javascript-open-in-new-tab-768x568.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/06\/javascript-open-in-new-tab.jpg 1160w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<h2>JavaScript window.open() to set hyperlink and target<\/h2>\n<p>This JavaScript one-line code sets the link to open the <em>window.open<\/em> method. The second parameter is to set the target to open the linked URL in a new tab.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-javascript\">window.open('https:\/\/www.phppot.com', '_blank').focus();\n<\/code><\/pre>\n<p>The above line makes opening a URL and focuses the newly opened tab.<\/p>\n<h2>JavaScript code to create HTML link element.<\/h2>\n<p>This method follows the below steps to open a URL in a new tab via JavaScript.<\/p>\n<ul>\n<li>Create an anchor tag (&lt;a&gt;) by using the <em>createElement()<\/em> function.<\/li>\n<li>Sets the href and the target properties with the reference of the link object instantiated in step 1.<\/li>\n<li>Trigger the click event of the link element dynamically created via JS.<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-javascript\">var url = \"https:\/\/www.phppot.com\";\nvar link = document.createElement(\"a\");\nlink.href = url;\nlink.target = \"_blank\";\nlink.click();\n<\/code><\/pre>\n<p><strong>Browsers support:<\/strong> Most modern browsers support the window.open() JavaScript method.<\/p>\n<p> <!-- #comments --> <\/p>\n<div class=\"related-articles\">\n<h2>Popular Articles<\/h2>\n<\/p><\/div>\n<p> <a href=\"https:\/\/phppot.com\/javascript\/javascript-how-to-open-url-in-new-tab\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n<\/p><\/div>\n<div id=\"social-icon\">\n<p>Share this page<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on June 25th, 2023. Web pages contain external links that open URLs in a new tab. For example, Wikipedia articles show links to open the reference sites in a new tab. This is absolutely for beginners. There are three ways to open a URL in a new tab. HTML anchor tags [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":134116,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-134115","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-updates"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/134115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=134115"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/134115\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/134116"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=134115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=134115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=134115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}