Web Programming

PSP browser support

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

With my broadband connection came a wireless network. So I tried browsing with my PSP. And it is a lot better than I expected. Except when browsing my own blog :(

I figured the easiest way to make it work was to send it the XHTML Basic version. So you should now be able to browse my site with a PSP without any hassle :D

Detecting the PSP browser**

Detecting a PSP is really easy. It sends a custom HTTP header: HTTP_X_PSP_BROWSER which contains the firmware version. Just check if that header is set. In PHP you just need to do:

if (isset($_SERVER['HTTP_X_PSP_BROWSER'])) $psp = true;

Now Google push AJAX development

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

I recently posted about BackBase, an expensive (for commercial use) AJAX development thingy*. Well Google have produced something similar for free Google Web Toolkit.

Although the end results are the same (as far as the user is concerned) there are important differences. The BackBase software is entirely client side. You write server stuff as normal, output BackBase code and the browser with JavaScript handles everything. The Google system is client and server orientated and odes more work on the server. The server also has to be running Java. It also has better browser support.

This could be a reason for me to learn Java, something I’ve managed to avoid for quite a while now…

* It’s actually an XML based markup language combined with a real time JavaScript processing engine.

German flip cards Google gadget

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

I’ve created a clever German flip card gadget for Google homepage. It displays a German word for a few seconds and then shows it’s translation. And then repeats with a new word. The vocabulary is very small at the moment but it will increase by at least one per day.

At the moment it also limits itself to 5 words per viewing. That is after showing five cards it loops (if it didn’t you’d never actually begin to memorise them).

Google Homepage

To use Google personalised homepage, you must have a Google account. When you have one, go to “Personalised Home” (links for that and to create an account are in the top right corner of Google’s homepage).

Adding the Gadget

From your personalised homepage, click “Add Content”, then “Create a Section” and then put the following URL in the box: http://www.oliverbrown.me.uk/gadgets/flip.xml . Then just click “Go” and you’re done :)

If anyone is interested I might extend the idea to be more flexible.

So where are the Google gadgets

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

If you use Google personalised homepage, you can add Google gadgets to them. A Google gadget is just an XML file (or more usually the XML output of some dynamic page) that is displayed. There is a well developed API and you can do quite a lot of nifty stuff with them. But they don’t seem to be that popular (searching for Google gadgets, with Google gets very few relevant results).

There are quite a few in their gadget directory but very little mention of them outside Google…

Multiple forms in ASP.NET

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

I previously ranted that being unable to have two ASP controlled forms in a single ASP.NET page is a serious flaw. They may be a way round it though. I would assume that ASP.NET can’t output two “smart” forms (the ones with runat="server") on the same page but I’m pretty sure that there is nothing stopping a HTML page itself actually holding two of them. Of course that could be wrong.

Essentially all you have to do is load the extra forms using AJAX and I think everything will work.

(I’m working a on a page (not in ASP.NET) that has a table of data with a status column. Each column needed to have a drop down box letting you change the status. Since the number of statuses is large I decided to have a link that AJAXly changed into the dropdown box and a button when you clicked it. Of course you could click all the links and not submit any of the forms leaving you with a page that actually has a bout 30 forms on it.)

BackBase really pushing AJAX

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

BackBase is another AJAX toolkit. This one is different though. It’s not really an AJAX toolkit, more a toolkit powered by JavaScript taking advantage of AJAX.

The clue is the price. Yes it has a price - $2000 to be exact. There is a “community edition” that is free for personal use though. Anyway, I don’t have time to run down all the features but basically it defines a whole new bunch of tags allowing you to create complicated content in a declarative HTML style way. These tags are then translated into proper XHTML on the fly by the back end JavaScript engine. Since the clever work is actually handled by the browser, you’re free to use whatever you like on the server (PHP, Ruby) including static HTML pages - outputting BXML is no different to outputting HTML. In fact BXML has a very ASP.NET feel to it and embedding BXML into an XHTML page along with ASP content could have the ultimate cleanness about it (syntax isn’t one of my complaints about ASP.NET).

It should be noted that Microsoft are working on Atlas which could be something very similar but I haven’t looked into it… It all seems very clever.

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…

Even simpler searching

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

I decided to take my idea for nicer searching in Wordpress one step further. Now if you try to get to a page that doesn’t exist it automatically redirects to the search page. So http://www.oliverbrown.me.uk/galaxia will return search results for “galaxia”.

If you want to do it yourself, you need to do two things. First you need to redirect error pages to a custom PHP page so add the following line to the top of your .htaccess file: ErrorDocument: 404error.php Now you need to create 404error.php and make it redirect to the search page by adding the following to it: header('Location: http://' . $_SERVER['HTTP_HOST'] . '/search.php?s=' . $_SERVER['REDIRECT_SCRIPT_URL']); After doing this it had me thinking about status codes. By default that will generate a 302 Found HTTP status code. A bit of reading led me to decide that 303 See Other was a better response since the request might correspond to a page in the future (and presumably a relevant page) but for now should be redirected. 3XX are somewhat confusing.

Anyway if you want that behaviour, add this line too: header('Status: 303 See Other'); Finally, you should think a little before implementing this since any random page accessed could now be cached by proxy servers and search engines etc. since the server is no longer sending a 404 Not Found code.

ASP.NET sucks

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

I recently bought a book about ASP.NET: Pro ASP.NET in C# by Apress, mainly because when I was looking for jobs there were lots of ASP.NET jobs advertised. And I have to say I have no idea why. Part of the problem may be that is book isn’t very good (there are bits of vague contradictions and a general obsessive (and inaccurate) preachiness about it) but I think there are major limitations to ASP.NET.

Firstly the inability to post to a different page. Who the hell decided that was good idea? I know it can be faked but that’s just silly. And you can only really have one form on a page. Well you can only have one “rich” form that ASP.NET can access in a clever and high level way.

I’m assuming people will disagree with me (if not, why is it so popular). If you do, please explain why ASP.NET is supposed to be so amazing because I don’t see it…

Updated - Clarifying my position on ASP.net

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.