Computers

Silly things with JavaScript closures

Oliver Brown
— This upcoming video may not be available to view yet.

From a theoretical programming point of view, JavaScript is immensely cool. You can do some amazing things with it. Although I’m not entirely sure whether you should.

For example I had a bunch of elements on a page I needed to update using AJAX. I needed a function I could pass the URLs and ids of elements to replace with those URLs and then have it perform each replacement in turn (I’ve seen IE have problems with simultaneous AJAX requests).

First I replace a simple replace_id function that accepts three arguments. An element id, a URL to GET to replace its contents with and finally a function to be called when it’s all completed.

And then things got silly.

function chain_replace(urls, ids) {
    id = 0;
    next_id = function() {
        if (id < = ids.length) {
            return function() {
                replace_id(urls[id], ids[id++], next_id());
            }
        }
    };
    next_id()();
}

Now the next_id()(); bit towards the end should be a clue that something a little odd is going on. But I must confirm that this code does actually work. With enough arguments it might make the browser explode with some sort of call stack problem though…

Windows XP Media Center Edition 2005

Oliver Brown
— This upcoming video may not be available to view yet.

I’m getting a new computer. Well most of a new computer. And I’ve been considering whether to get Windows Media Centre Edition or not.

The first two versions of MCE were rather lacking but after reading a lot I’ve decided 2005 is actually quite cool.

What is Windows XP Media Center Edition 2005?

Good question. One I didn’t know the answer to until recently. It’s Windows XP a with range of new utilities for working with video, music and images (as well as bits of hardware associated with them) all wrapped inside one interface. The idea is to have your computer as the hub for your whole home entertainment system.

Digital Video Recording (DVR)

The most useful component is the built in DVR software (sometimes called Personal Video Recorder or PVR). Plug your TV into your TV tuner card and Windows can record stuff straight to your hard drive. But of course most TV tuners come with software to do this. Well MCE does it better to be honest. And you can also get a hardware bundle (ready built systems come with it) that includes a IR blaster. Basically it’s an infra-red transmitter you stick to the front of your set top box (Sky, cable, whatever) to allow your MCE computer to change channels.

Disk space

The lowest quality recording takes up between 1Gb and 1.5Gb per hour. Reasonable hard drives these days are about 200Gb which gives you about 100 hours of video (leaving space for other stuff). Not really suitable for storage but it does allow you to burn things to DVD. Most of the time. The software apparently supports any content restriction specified in incoming media and won’t let you copy such content of the computer that made it.

But it’s still a computer, right?

MCE is actually Windows XP Professional underneath. It took a while to confirm (most references are vague about whether it’s XP Home or XP Pro) but I did find a page on Microsoft’s website saying it’s XP Pro. This means you can do everything with it that you can normally do with a PC.

One final note… you could always install MCE on a Mac.

JavaScript debugging with Internet Explorer

Oliver Brown
— This upcoming video may not be available to view yet.

Ever had a problem with JavaScript? If you don’t realise that’s a rhetorical question then go away now…

I had a simple script that worked fine in Firefox and Opera but not in IE. I got the amazingly useful message “Object expected” at line 0, character 0. So after looking around for a bit I found the Microsoft Script Debugger. And it’s actually quite good. Not as convenient as Firefox (since most of my problems are syntax errors) but it solved my problem. And in a way, I can blame Firefox and Opera for the problem. Maybe.

“class” is a reserved word in JScript (IE’s JavaScript engine) but it isn’t in Firefox and Opera. Perhaps it should be. Then perhaps not since contrary to popular belief, JavaScript (or technically ECMAScript) is not an object orientated language (at least not in the traditional sense), it’s a prototype language.

The main difference is a prototype language does not have classes (which is why “class” is not a reserved word in Firefox or IE), objects are given functionality on a per instance basis.

All this is rather academic though. If you need to debug JavaScript in IE for whatever reason, get the debugger. And ignore the link that says there is a newer version available - it’s pointing to Visual Studio .NET 2003.

Switching to a Mac is getting easier

Oliver Brown
— This upcoming video may not be available to view yet.

Unless you’ve been living under a rock for the past couple of weeks (possible admittedly - you could have been on holiday) you’ve probably heard that you can now run Windows XP on an Intel Mac using Boot Camp. This could be a great step forward for Apple with regards to capturing Windows users.

Firstly I don’t think Apple will capture the casual market, at least not in the near future. However much certain Mac enthusiasts may claim Macs are easier to use, a Windows machine does everything the casual user wants it to do (and because of the market share of Microsoft it does it the way the expect it to. Not only that but some of the details about installing Windows on a Mac are too much for the casual user (look for the confused look when you explain to them that if they use an NTFS partition to put Windows on the Mac won’t be able to write it. Then see how they laugh when you mention FAT32 as an alternative). The slightly more technical fraction of the market is much more obtainable.

Those with enough confidence with computers to manage the transition may now be willing to make the change. Before, buying a Mac was a big step. Unless you wanted to have two computers it meant giving up a lot. Now you can have the best of both worlds with just one computer. For developers this could be great. With the increasing success of Mono as a cross platform development environment, have a single computer to test everything on (whether it’s been done or not I’m sure it’s technically feasible to run Linux on an Intel Mac). This will ultimate increase the number of developers able to work with Macs and the whole things spirals outwards.

Perhaps.

Going more mobile

Oliver Brown
— This upcoming video may not be available to view yet.

I announced limited support for mobile devices viewing the blog recently. That support basically only covered phones with Opera Mini.

Well now I have a WML theme installed so you should be able to view the site with any WAP device.

You can force WML output with any browser though (although most browsers do not understand WML).

Detecting Mobile Phones

You’re first instinct is probably to check the user agent. Although it’s true there are fairly consistent ways to detect a phone from the user-agent there is a better way.

One of the many under-utilised headers that browsers always send (well 99.99% of browsers you encounter will) is called Accept. This is just a list of MIME types that the browser can handle. Since all mobile phones (as well as PDAs can display WML pages, we’ll use this as the basis to detect mobile devices. The MIME type for WML pages always seemed rather odd to me: text/vnd.wap.wml.

if (strpos($_SERVER['HTTP_ACCEPT'], 'text/vnd.wap.wml') !== false) $mobile = true;

Now we need to find out if the phone can accept XHTML Basic (or XHTML Mobile Profile or whatever) in pretty much the same way:

if (strpos($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml') !== false) $xhtml = true;

Many “real” browsers are inconsistent with regards to the MIME type for XHTML. As far as I know since mobile phones do not have any sort of backwards compatibility issues they all use the proper application/xhtml+xml.

XHTML Compliant - Thrice!

Oliver Brown
— This upcoming video may not be available to view yet.

I tried validating the markup and my site and found a few errors (nobr tag not allowed, span tag not allowed inside a ul and few others) so I fixed them and I’m XHTML 1.1 compliant once again. That is the home page and a few random pages I tried are compliant. There are probably some posts with random stuff that isn’t…

The main reason I did it was for the sake of any mobile browsers that might complain really loudly about bad markup. So I started reading about XHTML Basic what exactly was and wasn’t allowed. Well I couldn’t find anything useful so I just tried validating as XHTML Basic 1.0. I had to remove script tags, style attributes, replace i with em and again, a few other minor things. The end result is that an XHTML Basic version of the site is available to mobile browsers. Since I don’t have a mobile browser to test it on I can’t guarantee I’m detecting them properly yet. If you want to see what it looks like though, just go to Oliver Brown - Basic.

Then I discovered that it’s mainly PDAs that use XHTML Basic and that mobile phones tend to want XHTML Mobile Profile (XHTML MP also called WAP 2.0). Just changing the doctype was enough to get XHTML Basic to validate as XHTML MP. You can check it out at Oliver Brown - Mobile (it looks the same as Basic).

Just to let you know, when browsing the other versions manually all the links bring you back to the normal site - you actually need a browser detected as being a mobile phone for it to work properly.

StepMania

Oliver Brown
— This upcoming video may not be available to view yet.

Think of this as an introductory post into something I may mention more in the future. This way I can just send people here instead of explaining everything from scratch every time :P

Ever heard of Dance Dance Revolution (Dancing Stage in Europe)? Well it’s a funky arcade game you control by “dancing”. Arrows scroll up the screen and when they reach the top you press the corresponding button.

The really cool thing is a an open source PC version of it called StepMania. Generally StepMania has more features and allows you to add as many songs as you want (assuming you can get the media files and step charts). In fact there are arcade machines around that are actually a computer running StepMania inside a DDR cabinet (which is a little overwhelming to play because of the sheer number of songs available.

Random traffic according to Analytics

Oliver Brown
— This upcoming video may not be available to view yet.

Firstly, after using it for a week I’ve concluded Google Analytics is good. I can’t reasonable access any of it on a dial-up connection but that should be an increasingly small problem. (Increasingly small? Almost as good as “it’s looking more and more less likely”.)

One thing it reveals is random search engine traffic I get for almost no reason. I posted about the April fools gag on GameFAQs and I’m now on the second page of Google if you search for GameFAQs. The funny (although understandable) bit it is that it’s my internal search page for GameFAQs that is actually in the results.

Putting dating in context

Oliver Brown
— This upcoming video may not be available to view yet.

It’s that day again, April 1st, and boy the internet make it more fun. And of course Google are at again. Personally I don’t think they’ll ever beat Pigeon-Rank but they have to try.

The first thing I have to point out about Google is be very careful around April 1st. They have launched a number of genuine services on April 1st (including Gmail) that turned out to be real. None the less I’m fairly sure Google Romance is less than sincere…

And another thing, GameFAQs have decided it’s bad to cheat.