{"id":67,"date":"2004-11-29T13:48:45","date_gmt":"2004-11-29T11:48:45","guid":{"rendered":"http:\/\/www.maratz.com\/blog\/archives\/2004\/11\/29\/setting-target_blank-to-external-links\/"},"modified":"2005-02-07T03:06:28","modified_gmt":"2005-02-07T01:06:28","slug":"setting-target_blank-to-external-links","status":"publish","type":"post","link":"http:\/\/www.maratz.com\/blog\/archives\/2004\/11\/29\/setting-target_blank-to-external-links\/","title":{"rendered":"Setting <code>target=&#8220;_blank&#8221;<\/code> to external links"},"content":{"rendered":"<p>If you want your pages to be valid  XHTML-Strict, but you also wish to open external links in new window, the following snippet is doing just that\u2013sets attribute <code>target=\u201c_blank\u201d<\/code> to all external links in a document to keep your code valid XHTML-Strict. <!--more--> You\u2019ll just have to change variable <code>yourURL<\/code>. Don\u2019t enter <code>http:\/\/www.<\/code> part of your domain, otherwise something like <code>http:\/\/sample.com\/<\/code> will be treated like any other external link.<\/p>\n<pre>\/\/ change your domain name:\r\nvar yourURL = \"sample.com\";\r\nfunction outLinks() {\r\n  var outLink;\r\n  if (document.getElementsByTagName('a')) {\r\n    for (var i = 0; (outLink = document.getElementsByTagName('a')[i]); i++) {\r\n      if (outLink.href.indexOf(yourURL) == -1) {\r\n        outLink.setAttribute('target', '_blank');\r\n      }\r\n    }\r\n  }\r\n}\r\nwindow.onload = function() {\r\n    outLinks();\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want your pages to be valid  XHTML-Strict, but you also wish to open external links in new window, the following snippet is doing just that&#8212;sets attribute <code>target=&#8220;_blank&#8221;<\/code> to all external links in a document to keep your code valid XHTML-Strict<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/67"}],"collection":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":0,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}