<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="https://mg.to" xmlns:dc="https://purl.org/dc/elements/1.1/">
<channel>
 <title>mg.to - Your &amp;lt;body&amp;gt; is in your &amp;lt;head&amp;gt; - Comments</title>
 <link>https://mg.to/2006/04/29/your-body-is-in-your-head</link>
 <description>Comments for &quot;Your &lt;body&gt; is in your &lt;head&gt;&quot;</description>
 <language>en</language>
<item>
 <title>IE Developer Toolbar</title>
 <link>https://mg.to/2006/04/29/your-body-is-in-your-head#comment-169</link>
 <description>&lt;p&gt;Hi
I looked up debugbar.com that you mentioned, and it appears to be a licensed software. I just came across a DOM viewer plugin by MS itself - &lt;a href=&quot;https://www.microsoft.com/downloads/details.aspx?FamilyID=e59c3964-672d-4511-bb3e-2d5e1db91038&amp;amp;displaylang=en&quot;&gt;IE Developer Toolbar&lt;/a&gt;.
It did the job perfectly, and for free!
Regards
Ashutosh&lt;/p&gt;</description>
 <pubDate>Sat, 13 May 2006 05:11:48 +0000</pubDate>
 <dc:creator>Ashutosh</dc:creator>
 <guid isPermaLink="false">comment 169 at https://mg.to</guid>
</item>
<item>
 <title>Your &lt;body&gt; is in your &lt;head&gt;</title>
 <link>https://mg.to/2006/04/29/your-body-is-in-your-head</link>
 <description>&lt;p&gt;I was chasing down a bizarre bug. My JavaScript code was working fine in IE, Firefox, and Safari, until I tried using it here on &lt;a href=&quot;https://mg.to/&quot;&gt;mg.to&lt;/a&gt;. It still worked in Firefox and Safari, but it blew up in IE, with behavior that made no sense at all. I was seeing duplicate copies of DOM elements I created using my &lt;a href=&quot;https://mg.to/2006/02/27/easy-dom-creation-for-jquery-and-prototype&quot;&gt;DOM creation plugin&lt;/a&gt; for &lt;a href=&quot;https://jquery.com/&quot;&gt;jQuery&lt;/a&gt;, and all kinds of strange behavior. It was almost as if something was fundamentally wrong with the way the DOM was working.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m usually pretty good at tracking down problems, but this one had me stumped. I wanted to look around the the DOM structure, but since this was IE, I couldn&amp;#8217;t use any of the usual Firefox plugins such as the &lt;a href=&quot;https://www.mozilla.org/projects/inspector/&quot;&gt;DOM Inspector&lt;/a&gt; or &lt;a href=&quot;https://www.joehewitt.com/software/firebug/&quot;&gt;FireBug&lt;/a&gt;. Then I found a great tool for IE troubleshooting, the &lt;a href=&quot;https://www.debugbar.com/&quot;&gt;DebugBar&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I looked at the DOM with the DebugBar, and my jaw dropped when I saw this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://mg.to/images/body-in-head-0.png&quot; alt=&quot;DebugBar DOM&quot; title=&quot;DebugBar DOM&quot; /&gt;&lt;/p&gt;

&lt;p&gt;(This screen shot and the others below are from simplified test cases.)&lt;/p&gt;

&lt;p&gt;This had to be impossible! The document &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; was &lt;em&gt;inside&lt;/em&gt; the &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag, which in turn was inside the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;. The latter I&amp;#8217;d expect, but why was &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; not a &lt;strong&gt;sibling&lt;/strong&gt; of &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; as it should be? And how was &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; involved in this?&lt;/p&gt;

&lt;p&gt;I got lucky with some searches and found these articles by &lt;a href=&quot;https://weblogs.asp.net/justin_rogers/&quot;&gt;Justin Rogers&lt;/a&gt; from the IE team:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://blogs.msdn.com/ie/archive/2005/08/29/457667.aspx&quot;&gt;All your &amp;lt;base&amp;gt; are belong to us&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://weblogs.asp.net/justin_rogers/archive/2005/08/30/424085.aspx&quot;&gt;Implied tags in the IE HTML parser&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://weblogs.asp.net/justin_rogers/articles/423843.aspx&quot;&gt;BASE tag changes in IE 7&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This part of the &amp;#8220;Implied tags&amp;#8221; article seemed to explain what was going on:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The set of implied rules has impacts in other areas as well. You can, for instance, end up using document.writeln to prematurely terminate your HEAD element and move a bunch of stuff out into the BODY. So, if you are doing inline document writes you should probably do them where you want the content to go. Writing the content out in script blocks that appear in the head is the wrong way to go about it. You could hook up to some events or have a container element that you write into, and that is acceptable, but with inline writes you could get unexpected behavior.&lt;/p&gt;
  
  &lt;p&gt;Recently I noticed a site that was doing a document.writeln in their HEAD element about half-way through the head content. End result? Well, the content got moved into the BODY element and the object model tree for the page was completely wrong. Good thing they weren&amp;#8217;t navigating the object model looking for stuff and good thing the extra META/LINK elements weren&amp;#8217;t being used as well. With a static parse of the page you wouldn&amp;#8217;t even notice these problems, but when DHTML becomes involved it can change the structure of your document on the fly and rewrite what the object model tree looks like.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Indeed, this site runs on &lt;a href=&quot;https://drupal.org/&quot;&gt;Drupal&lt;/a&gt;, and Drupal 4.6.x does use a &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag. (The forthcoming Drupal 4.7 eliminates the &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag.)&lt;/p&gt;

&lt;p&gt;Justin&amp;#8217;s examples showed an unclosed &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag like this:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-html&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;https://december.com/html/4/element/base.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;base&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;href&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/a&gt; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That should be OK; the &lt;a href=&quot;https://www.w3.org/&quot;&gt;W3C&lt;/a&gt; HTML specification &lt;a href=&quot;https://www.w3.org/TR/html4/struct/links.html#h-12.4&quot;&gt;defines the BASE element as EMPTY&lt;/a&gt;, so it shouldn&amp;#8217;t require any kind of closing tag (except for XHTML compatibility).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag that Drupal generates is self-closing in the XHTML style:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-html&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;https://december.com/html/4/element/base.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;base&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;href&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&quot;https://mg.to/&quot;&lt;/span&gt; /&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/a&gt; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, IE6 does not seem to recognize that the tag is closed (or EMPTY), and it puts everything after that &lt;em&gt;inside the BASE element&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;(You may also notice that strictly speaking, Drupal&amp;#8217;s &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag is incorrect. It should include a filename, but it seems to work OK without it&amp;#8212;except for the IE problem.)&lt;/p&gt;

&lt;p&gt;On a hunch, I tried closing the tag the old fashioned way:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-html&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;https://december.com/html/4/element/base.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;base&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;href&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&quot;https://mg.to/&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/base&amp;gt;&lt;/span&gt; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and presto! Everything started working, and DebugBar revealed that the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; elements were siblings, both direct children of &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; as expected.&lt;/p&gt;

&lt;p&gt;The bottom line: Every HTML document in the world that uses a &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag is being parsed in this odd way by IE, unless an explicit closing &lt;code&gt;&amp;lt;/base&amp;gt;&lt;/code&gt; tag is used. It doesn&amp;#8217;t affect ordinary HTML rendering, but any kind of DOM manipulation may go haywire.&lt;/p&gt;

&lt;p&gt;Here are the three test cases. First, the unclosed/empty &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://mg.to/images/body-in-head-1.png&quot; alt=&quot;Unclosed BASE tag&quot; title=&quot;Unclosed BASE tag&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The XHTML-style &lt;code&gt;&amp;lt;base /&amp;gt;&lt;/code&gt; tag is no better:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://mg.to/images/body-in-head-2.png&quot; alt=&quot;Self-closing BASE tag&quot; title=&quot;Self-closing BASE tag&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And the one that works, with a closing &lt;code&gt;&amp;lt;/base&amp;gt;&lt;/code&gt; tag:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://mg.to/images/body-in-head-3.png&quot; alt=&quot;BASE tag with closing tag&quot; title=&quot;BASE tag with closing tag&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There is one remaining problem. If you validate your pages as HTML 4.01 Transitional, there is no way to use a &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag that works correctly in IE and also validates. The validator barfs on the closing &lt;code&gt;&amp;lt;/base&amp;gt;&lt;/code&gt; tag, because it figures the tag is already closed (being an EMPTY element).&lt;/p&gt;

&lt;p&gt;If you use XHTML 1.0 (either Transitional or Strict), then you can use the closing &lt;code&gt;&amp;lt;/base&amp;gt;&lt;/code&gt; tag and it will validate. Since most people who validate their pages are probably using XHTML anyway, this shouldn&amp;#8217;t be a problem for many.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://validator.w3.org/check?uri=http%3A%2F%2Fmg.to%2Ftest%2Fbase4.html&quot;&gt;Invalid HTML&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://validator.w3.org/check?uri=http%3A%2F%2Fmg.to%2Ftest%2Fbase3.html&quot;&gt;Valid XHTML&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, the W3C&amp;#8217;s &lt;a href=&quot;https://www.w3.org/TR/xhtml1/#guidelines&quot;&gt;XHTML/HTML Compatibility Guidelines&lt;/a&gt; offer this warning:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&amp;#8230;use the minimized tag syntax for empty elements, e.g. &lt;code&gt;&amp;lt;br /&amp;gt;&lt;/code&gt;, as the alternative syntax &lt;code&gt;&amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;&lt;/code&gt; allowed by XML gives uncertain results in many existing user agents.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, that&amp;#8217;s just great. The only syntax that works in IE and validates is &lt;code&gt;&amp;lt;base&amp;gt;&amp;lt;/base&amp;gt;&lt;/code&gt;, but W3C warns against it. I haven&amp;#8217;t actually seen any problems caused by using this syntax with the &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag, though, even in old browsers. So for now, I&amp;#8217;m using it and hoping for the best.&lt;/p&gt;

&lt;p&gt;Major thanks are due to the DebugBar for pointing me toward the problem, and Justin Rogers for explaining it.&lt;/p&gt;
</description>
 <comments>https://mg.to/2006/04/29/your-body-is-in-your-head#comments</comments>
 <category domain="https://mg.to/topics/programming/html">HTML</category>
 <category domain="https://mg.to/topics/programming/javascript">JavaScript</category>
 <category domain="https://mg.to/topics/programming">Programming</category>
 <pubDate>Sat, 29 Apr 2006 03:16:05 +0000</pubDate>
 <dc:creator>Michael Geary</dc:creator>
 <guid isPermaLink="false">160 at https://mg.to</guid>
</item>
</channel>
</rss>
