Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

minidom self-closes empty SCRIPT tags

Jerry Stratton, September 16, 2010

I panicked a bit today when I went to Automatically grab flavor text snippets in Nisus in IE 8, and then Firefox 3.6.10, and discovered that the page stopped displaying just before the code sample!

The immediate issue turned out to be that those browsers still don’t support self-closing <script> tags. Safari 5.0.2 does, and it’s valid XHTML, so I hadn’t noticed it.

However, I knew that when I made the Django template that contains this tag. It looks like this in the template file:

  • <script type="text/javascript" src="{{ centralstart }}/library/scripts/code.js"></script>

And I verified that it was working on every other page I could think of that held it.

The answer turned out be that the failing page also contained a media file, and, being a lazy programmer, I don’t usually manually place each media file on a page, I just parse the XHTML, distribute the media evenly among the elements, and then reconstitute the XHTML.

Python’s minidom was—correctly, this is what should happen with empty elements—seeing that the script element had no content and self-closing it.

The solution is a further, very minor, hack: put a space between <script> and </script> so that the element does have content and can’t self-close:

  • <script type="text/javascript" src="{{ centralstart }}/library/scripts/code.js"> </script>

I’d bold the new character, but it’s a space.

  1. <- San Diego Hamfest
  2. GeekTool color codes ->