carriage returns are false metal - Mon Nov 16, 2009
Ahh, carriage returns how I love thee. Today I managed to waste around 20 minutes banging my head against some very simple Javascript used to open a new window when the user clicks a link:
<a href="http://codegrunt.com" onclick="window.open(this.href); return false;">this is a link</a>
Trivial code, user clicks link and new window opens up. If life was only that simple - instead of the desired behaviour, the original URL was also firing and causing the originating window to load the URL as well. Arrrgh.
Can you see the bug? It is a trick question really as you would have probably have to view the source to notice it. The problem was a stray carriage return after "return" which causes Javascript to skip the return value of "false" and happily follow the link in both the current page and a new window. Now normally this would have been immediately obvious to me but my IDE is set up to nicely linewrap long lines which normally is pure awesome but unfortunately tricked me today when high LCD contrast (and perhaps not enough coffee) made the grey line wrap less obvious.
I hate these situations, something so simple going inexplicably astray. It becomes so easy to spiral off on dangerous tangents, "Oh no, maybe NoScript is doing something funny. . ." or "Crap, I bet something buried in an external library is messing with events!" Time consuming goose chases which could easily create their own mess as you wade frantically through trying to solve an issue that seems both impossible and ridiculous.
It has been a while but I again know how it feels to get punked by carriage returns.
topics: javascript, programming
Experienced PHP web developer.
$35 an hour, no job too small.
There is no replacement for integrity and experience.