It all started…

So, I was thinking on my way home how clunky wanting to add text to a page with JavaScript. My use-case is this: I want to add the current year to my page. But because I’m just using HTML, I don’t have any server-side ability to add this. So I’m using:

<span id="thisYear"></span>

And then later…

<script type="text/javascript">
// set copyright year
$("#thisYear").text(new Date().getFullYear());
</script>

So I wanted a different way to do it. I looked at using an empty tag (<var id=”thisYear” />) but Firefox doesn’t seem to like that, because it was swallowing up text behind that. I did a search for empty tags and found this article on empty elements.

The question arises whether “Tags for Empty Elements” in XML, i.e. things like <hr /> (or <hr />), really comply with SGML rules. The SGML Handbook seems to say they don’t. The start tag syntax there (p. 314) says that between the tag name (“generic identifier” in SGML terminology) and the closing “>” (“tagc”, for tag close), only attribute specifications and whitespace is allowed.

So it seems that XML empty tags don’t really comply with SGML rules!
So what is Firefox really doing?

Javascript Optimizations

I was looking at kangax‘s suggestions for Google Closure compression techniques and I wanted to know how UglifyJS handled it.

Input:
var i = 10000,
j = 0.1,
k = /[\w]/,
m = new Array(1, 2, 3);
throw 0.1;

Closure:
var i=1E4,j=0.1,k=/[\w]/,m=[1,2,3];throw 0.1;

UglifyJS:
var i=1e4,j=.1,k=/[\w]/,m=new Array(1,2,3);throw.1

Interesting that UglifyJS doesn’t compress the Array declaration, but does compress 2 other statements that Google Closure doesn’t/won’t.

In UglifyJS’s README it states that Google Closure “runs terribly slow”. Why is this an issue? Won’t you only compress when pushing to prod?

navigator.appVersion

Wow. This is messed up. Here is a table of the browsers I have at my disposal and the returned value from the JavaScript variable “navigator.appVersion”.

Browser navigator.appVersion
Chrome 5.0 5.0
Opera 10.00 9.80
IE 8.0 4.0
IE 7.0 4.0
IE 6.0 4.0
Safari 4.0 5.0
Safari 3.2 5.0
Firefox 3.6 5.0
Firefox 3.5 5.0
Firefox 2.0 5.0

Ok browser makers. Let’s start over. I don’t think any site is using the navigator.appVersion anymore so now is as good of time as ever to start fresh. What do you say?

Comments on Optimizing HTML

I was reading Juriy Zaytsev‘s (@kangax) article “Optimizing HTML” and had a few comments.

1. I have used <!CDATA[…]> sections when I’m embedding “<” and “>” but I think that’s just because I’ve used XSL before and they have caused problems when performing transformations. But I’ll try to remove it from my HTML as he suggests.

2. If I don’t have a URL to use in the href attribute, I’ll still use “javascript:void(0)” because I really don’t like the hash being added to my URL. Perhaps I should just add some jQuery code:

$(“a[href=’#’]”).click(function(){return false;});

That’s 49 characters, so I would have to change only 3 links to use the hash instead of “javascript:void(0)” to save space.