{"id":102,"date":"2009-01-13T13:22:02","date_gmt":"2009-01-13T18:22:02","guid":{"rendered":"http:\/\/www.blizzarddigital.com\/blog\/?p=102"},"modified":"2010-04-18T19:47:00","modified_gmt":"2010-04-18T23:47:00","slug":"the-target-attribute-and-strict-xhtml","status":"publish","type":"post","link":"https:\/\/www.blizzarddigital.com\/blog\/the-target-attribute-and-strict-xhtml\/","title":{"rendered":"The Target Attribute and Strict XHTML"},"content":{"rendered":"<p>So, you&#8217;ve decided to start creating your web sites with valid strict XHTML 1.0 and CSS. Your client wants a &#8220;links&#8221; page containing none other than links to other web sites. So you oblige and, to keep your client&#8217;s visitors on his site, you make the links open in a new window. So you throw in <code>target=\"blank\"<\/code> and you&#8217;re done. Just run it through the W3 validator&#8230;<\/p>\n<p><em>There is no attribute &#8220;target&#8221;?\u00a0What gives?!<\/em><\/p>\n<p>Yes, indeed, <code>target<\/code> is not a valid attribute in XHTML 1.0 Strict. There&#8217;s really only one way around it of which I&#8217;m aware, and that&#8217;s by using Javascript. My preferred method is as follows:<\/p>\n<div class=\"code-example\"><code>&lt;a href=\"http:\/\/www.blizzarddigital.com\/blog\/\" onclick=\"window.open(this.href, 'OffSite').focus(); return false;\"&gt;<\/code><\/div>\n<p>This opens the URL in\u00a0a new window and brings it into focus if the user had previously opened an &#8220;OffSite&#8221; window and didn&#8217;t close it.<\/p>\n<p>You would still put the desired URL in the <code>href<\/code> attribute just like with any hyperlink. That way, if the visitor for some reason has Javascript disabled, the link still functions correctly. It just wouldn&#8217;t open in a new window.<\/p>\n<p>One caveat though\u2014in Firefox 2 and later with default settings, this code causes the linked site to open in a new tab, not a new window. If you find this to be a problem, there is a way around it, and that&#8217;s by adding the window options parameter such as:<\/p>\n<div class=\"code-example\"><code>&lt;a href=\"http:\/\/www.blizzarddigital.com\/blog\/\" onclick=\"window.open(this.href, 'OffSite', 'directories=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,toolbar=yes').focus(); return false;\"&gt;<\/code><\/div>\n<p>Those are all the options required to make the window appear normally with default tool and menu bars\u2014kind of a pain. At this point, you may want to move the code into an external Javascript file. The function I use typically looks like the following:<\/p>\n<div class=\"code-example\"><code>function OpenOffSite(a)<br \/>\n{<br \/>\nwindow.open(a.href, \"OffSite\", \"directories=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,toolbar=yes\").focus();<br \/>\nreturn false;<br \/>\n}<\/code><\/div>\n<p>Then the <code>onclick<\/code> attribute of your anchor would look like so:<\/p>\n<div class=\"code-example\"><code>&lt;a href=\"http:\/\/www.blizzarddigital.com\/blog\/\" onclick=\"return OpenOffSite(this);\"&gt;<\/code><\/div>\n<p>Your links now open in a new window! Welcome to the world of XHTML conformity. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to open links in a new window because target is not a valid attribute in XHTML 1.0 Strict.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[36,40,41,6,183,19],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-web-standards","tag-javascript","tag-new-window","tag-target","tag-web-design","tag-web-standards","tag-xhtml"],"_links":{"self":[{"href":"https:\/\/www.blizzarddigital.com\/blog\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blizzarddigital.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blizzarddigital.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blizzarddigital.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blizzarddigital.com\/blog\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":12,"href":"https:\/\/www.blizzarddigital.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/www.blizzarddigital.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/www.blizzarddigital.com\/blog\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blizzarddigital.com\/blog\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blizzarddigital.com\/blog\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}