I’ve now written my first AJAX code: if you go to a random web page in my book/game database, you should be presented with a list of blog posts that refer to that item. At least assuming that I haven’t accidentally used functionality that your favorite browser doesn’t support, which I hear is easy to do with JavaScript; fortunately, Internet Explorer seems to be the most likely candidate, and my CSS is already broken there, so I should be safe enough. (I’ve only tested under Firefox and Safari 3.)

It was fun and not too hard, all things considered. I didn’t have any prior JavaScript experience, but I figured that googling would quickly turn up instructions for how to do what I wanted. Which didn’t seem to be the case in the first 15 minutes or so of searching, but I remembered getting the idea from an example in the REST book, so I looked that up, googled some of the more specific language constructs I had questions with, and had something working in another hour or so.

Aside from stupid mistakes, the main initial difficulty that I ran into was in the guards against cross-site scripting vulnerabilities: my blog has its own domain, while my database is in bactrian.org. (And I was doing my initial prototyping on my home computer, which is a different physical machine.) The easiest way to get around that seemed to be to set up a proxy (or rather, two of them, one on my home computer and one on bactrian.org); a bit of mod_proxy configuration later and my prototype worked on my home computer.

I copied the prototype over to bactrian.org, and updated paths; it stopped working, again giving me a frustrating error message related to cross-site scripting. I couldn’t figure out what was going on, and spent a quite frustrating hour or so alternating between googling for help and trying to install and run a JavaScript debugger. (For some reason, the Venkman package in Ubuntu didn’t work for me.) Eventually, though, I remembered one more path that I would need to translate when moving the prototype; I changed it, and the prototype worked in the deployment environment.

After which it was a simple matter of programming to make the change and update the tests. Most of the work was in the latter: I have the web page skeleton abstracted fairly well out of the tests, but even so I have to modify a few different places if I change the output in a way that affects all pages. And I ran into one more road bump along the way, where inserting some white space in the HTML turned out to require me to change my JavaScript, but I figured that out pretty quickly.

So here we are. I’m really pleased with the results: there’s a big difference between being invited to click on a link to search for related blog posts (which may not even exist) and having a list of posts appear in front of you. This is the last change that I plan to make to the database for the time being (well, I might do a bit of trivial tweaking); a good change to go out on.

Random JavaScript-related thoughts:

  • Based on my limited experience, JavaScript is pleasant enough. I wasn’t impressed with its collections, but other than that the language behaved in the way I wanted, and it was quite easy to search through XML and pop some data into view.
  • I’m still mostly at sea if something goes wrong with my JavaScript. The console gives some basic help, but there were a couple of instances where I ran into a more serious problem and either wished that I could get more information out of the error or poke around data structures or something. Probably the debugger would have helped, if I’d gotten farther with using it.
  • I don’t have any acceptance tests for this, which makes me sad. It’s little enough JavaScript code that I’m happy to skip unit tests for it, but I really would like to be able to push a button and have reasonable confidence that I haven’t broken anything. (Especially since the functionality depends both on my JavaScript code and on WordPress’s behavior, so I’m going to have to manually test this every time I upgrade WordPress.) Some people on the XP list suggested some tools (Selenium in particular); maybe I’ll give that a try at some point.

Post Revisions:

There are no revisions for this post.