{"id":126,"date":"2006-11-06T16:08:53","date_gmt":"2006-11-06T16:08:53","guid":{"rendered":"http:\/\/lachy.id.au\/log\/2006\/11\/xhtml-script"},"modified":"2006-12-11T22:31:29","modified_gmt":"2006-12-11T22:31:29","slug":"xhtml-script","status":"publish","type":"post","link":"https:\/\/lachy.id.au\/log\/2006\/11\/xhtml-script","title":{"rendered":"Scripts in XHTML"},"content":{"rendered":"<p>I frequently come across people who aren\u2019t aware of the problems with using\r\n   scripts inside XHTML documents. One of these problems is particularly fatal\r\n\tbecause it can cause a well-formedness error, but it\u2019s not seen as\r\n\tan issue when documents are served as <code>text\/html<\/code>.\r\n<\/p><p>The problem is with the use of special characters like ampersands (<code>&amp;<\/code>), less-than\r\n\t(<code>&lt;<\/code>) and greater-than signs (<code>&gt;<\/code>). Only the first\r\n\t2 of those are actually fatal, but it\u2019s good practice to escape the latter as\r\n\twell.\r\n<\/p><p>Note: This is not an issue if you use external scripts in a separate file,\r\n\twhich is usally a better alternative anyway, but there are times when including\r\nscripts in the page itself is useful.\r\n<\/p><p>To illustrate the problem, here are some examples. The following is fine in HTML 4, but not in XHTML.<\/p>\r\n<pre><code>&lt;script type=&quot;text\/javascript&quot;&gt;\r\n  if (a &amp;&amp; b) {\r\n     \/\/ Oops! Ampersands cause well-formedness error in XHTML!\r\n  }\r\n&lt;\/script&gt;<\/code><\/pre>\r\n\r\n<p>For XHTML, that would need to be written like this.<\/p>\r\n\r\n<pre><code>&lt;script type=&quot;text\/javascript&quot;&gt;&lt;![CDATA[\r\n  if (a &amp;&amp; b) {\r\n  }\r\n]]&gt;&lt;\/script&gt;<\/code><\/pre>\r\n\r\n<p>Or this:<\/p>\r\n\r\n<pre><code>&lt;script type=&quot;text\/javascript&quot;&gt;\r\n  if (a &amp;amp;&amp;amp; b) {\r\n    \/\/ Oops! JavaScript syntax error in text\/html\r\n  }\r\n&lt;\/script&gt;<\/code><\/pre>\r\n\r\n<p>But now, when served as <code>text\/html<\/code> (not XML), both the <code>CDATA<\/code> section\r\n\tand the escaped ampersands are going to cause JavaScript syntax errors. You\r\n\tcan comment them out like this:<\/p>\r\n\r\n<pre><code>&lt;script type=&quot;text\/javascript&quot;&gt;\/\/&lt;![CDATA[\r\n  if (a &amp;&amp; b) {\r\n  }\r\n\/\/]]&gt;&lt;\/script&gt;<\/code><\/pre>\r\n\r\n<p>But it\u2019s easier to just admit you\u2019re not using XHTML and use HTML instead.<\/p>\r\n","protected":false},"excerpt":{"rendered":"Problems with using scripts inside XHTML documents.","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,4],"tags":[],"_links":{"self":[{"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/posts\/126"}],"collection":[{"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":0,"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"wp:attachment":[{"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lachy.id.au\/log\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}