title: Are <script> tags outside <head> and <body> permitted in HTML?
body: <p>Based on this blog post <a href="http://mathiasbynens.be/notes/async-analytics-snippet" rel="nofollow noreferrer">Optimizing the asynchronous Google Analytics snippet</a> and the comments, I have come up with this optimized Google Universal Analytics snippet that is shorter, runs faster (albeit slightly) and breaks IE6/IE7/oldIE8<code>*</code> compatibility.</p>
  
  <pre><code>+function(G,o,O,g){G.GoogleAnalyticsObject=O;G[O]||(G[O]=function(){(G[O].q=G[O].q||[]).push(arguments)});G[O].l=+new Date;g=o.createElement('script');g.src='//www.google-analytics.com/analytics.js';o.documentElement.appendChild(g)}(this,document,'ga');
  ga('create', 'UA-XXXX-Y', 'auto');
  ga('send', 'pageview');
  </code></pre>
  
  <p>However, when the snippet above is placed in the <code>&lt;head&gt;</code>, it inserts the Universal Analytics script as a child not of <code>&lt;head&gt;</code>, not of <code>&lt;body&gt;</code>, but of <code>&lt;html&gt;</code>, right between <code>&lt;/head&gt;</code> and <code>&lt;body&gt;</code>.</p>
  
  <p><img src="https://i.stack.imgur.com/FoBAD.png" alt="script between closing head tag and opening body tag"></p>
  
  <p>Although IE8+ and modern browsers seems to have no problem with that, I am still worried that it might break older mobile browsers or some other obscure and non-IE browser somewhere.</p>
  
  <p>Is there anywhere in the HTML specs that describe how a browser should react to tags placed outside <code>&lt;head&gt;</code> and <code>&lt;body&gt;</code>?</p>
  
  <p><code>*</code> Unpatched vanilla IE8 on Windows XP will abort page loading just like IE6 and IE7 and spew an <code>HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)</code> error. After installing a cumulative security update, starting with KB980182 first released in April 2010 and included in subsequent updates (KB982381, KB2183461, KB2360131, KB2416400, KB2482017, KB2497640, KB2530548, KB2559049, KB2586448, KB2618444, KB2647516, KB2675157, KB2699988, KB2722913, KB2744842, KB2761465, KB2792100, KB2809289, KB2817183, KB2829530, KB2838727, KB2846071, KB2862772, KB2870699, KB2879017, KB2888505, KB2898785, KB2909921), fixes the problem.</p>