ashleysheridan.co.uk

Blog

I would ask that when leaving your own comments you ensure you follow the blog terms & conditions. Any posts that break the terms will be removed, and could result in you being banned from posting comments again.

Debugging is a major part when writing software in any language, and Javascript is no exception. Sometimes though, even the most simple of code can create problems, and even fly in the face of logic and reality. Any of you who have read any of the Discword books by Terry Pratchett will understand exactly what I mean when I say I had a Hex moment.

The code itself was a simple function that was the receiver for some XML after an AJAX call. Now, almost all of the function was working correctly, and I was able to fully process the XML object which was returned. The final part of my function was meant to adjust some classes on a couple of div objects, just for aesthetic reasons. This last part however, was the one which didn't work. After a while of cursing, and staring, I realised that I was referencing variables which were not in my immediate scope. "No problem", I thought, "I'll just comment this if/else statement out and come back to it later." At least, that was the plan. No sooner had I commented out the if/else, than the whole function ceased to, well, function.

This was bizzare, as the only properties I was setting were class names of HTML tags, which weren't read anywhere else by any Javascript except this very if/else! I commented ut the lines inside the logic one at a time, just to catch the offending line, and had no results at all, not even when I'd commented out each and every line that wasn't part of the if statement. So, here I have one function with an empty if/else, which, as it's empty, can't do a single thing, yet when I remove it, the whole thing ceases to work properly. My situation was exactly that of Ponder Stibbons, who was able to discern no purpose to the mouse nest, yet when it was removed from Hex, it stopped, just like my function.

Eventually, I did solve it, but only by re-writing the function under a different name. I've no idea what happened, but I'm worried that if I look at it again too closely it might realise and break again!

I'm a big advocate of open source software, and try to bring it to the attention of others whenever I can. However, I often stop short at things like CMSs. My main gripe is that they always offer far more functionality than is ever wanted for one web site, and this power comes at the cost of being slower and more complex to alter. Now don't get me wrong, I do think these sorts of systems are perfect for a lot of cases, I'm just biased because I've already put together several custom solutions myself, and each time I've developed that way because the project required very niche functionality.

A recent spreadsheet tool I'm developing at work was born from a similar reason. Initially, an Excel spreadsheet was passed around via email in a round-robin, allowing several people to update it. While this did work, it was too slow, and unless everyone passed it on properly, it was hard to make sure everyone was kept up-to-date with changes. My first suggestion was Google Docs, as this is very stable these days, and offers all of the same functionality of Excel, with the added multi-user capabilities.

For various reasons, it was decided that an in-house solution should be developed instead, allowing full control of the appearance and functionality. So far, I've got it to a fairly usable state, although IE did rear it's ugly head in protestation over perfectly valid code. With the exception of these problems, it's all working fairly well, and across several browsers too; IE, Firefox, Safari, Opera, Konqueror & Epiphany. Now, it doesn't yet handle formulas (as it doesn't need to) or the selection of multiple cells, but for something that's been put together and debugged in the space of a couple of weeks, it's not bad going. It handles multiple documents, and even allows multiple users to edit the same document, and correctly sort out edit conflicts, all with the help of an AJAX interface. As the system is put to use I expect to make more changes, but that's the nature of all software.

Google Chrome has been with us for quite a couple of weeks now, but as yet I've seen nothing done to the public browscap.ini file. For those of you who don't know, this ini file is the defacto standard to use when browser sniffing, and countless visitor tracking systems use it across the world. What this has meant for me is that visitors coming to my site using Google Chrome have been showing as having unknown browsers, and unfortunately I didn't have the foresight to record the whole user agent string so that I could identify past browsers.

Because of this, I've made an attempt to modify the browscap.ini file to recognise the Chrome user agent strings in use. This was not as easy as it sounds, as there are more than one in use and Google itself only reports as having one, so go figure. I've allowed for the following user agents:

[Mozilla/5.0 (Windows; *; Windows NT 5.1; *) AppleWebKit/* (KHTML, like Gecko) Chrome/* Safari/*]
[Mozilla/5.0 (Windows; *; Windows NT 5.1 x64; *) AppleWebKit/* (KHTML, like Gecko) Chrome/* Safari/*]
[Mozilla/5.0 (Windows; *; Windows NT 6.0; *) AppleWebKit/* (KHTML, like Gecko) Chrome/* Safari/*]

You may note that these are not exactly the same as the user agents you will find online, but that is just because the unimportant parts have been replaced with the * wildcard. If the ini file had to take note of every combination of major and minor component of this browser, it would quickly turn into a very long list!

I've got a copy of the php_browscap.ini file on my server, and also a copy of the additions I made to the existing file, if you wish to just review and add these changes yourself.

November 2008
SMTWTFS
1
2345678
9101112131415
16171819202122
23242526272829
30