Mon Jul 31
A few months ago I asked the question whether it was possible for some Javascript to parse through a piece of HTML and render it all out using the “proper” DOM methods rather than the sledgehammer-to-crack-a-nut manner of innerHTML. The great Stuart Langridge commented that this woul open up a huge can of worms in case people put non-standard code into it. I agree.
Well, it seems there’s a way to do this from the other side, if you know what I mean. The clever chaps (and chapess) at Vivabit came up with this clever DOMBuilder script that allows you to build snippets of HTML code using the DOM in an almost-easy manner. Well, it’s certainly easier that createElement-ing and appendChild-ing everything.
And it’s also another neat use of the Javascript Object Notation thingamajig. Clever.
Mon Jul 24
I’ve been playing with the excellent Google Sitemaps facility (yes, you guessed it, it’s in beta) and I’m impressed. It’s easy to use, and provides some really nice simple statistics for those that are that way inclined. For example, here’s my top ten search queries:
| Number |
Search |
Average position |
| 1 |
animals |
11 |
| 2 |
sport |
17 |
| 3 |
chris taylor |
10 |
| 4 |
wibsite |
8 |
| 5 |
datatable class |
9 |
| 6 |
animals in sport |
10 |
| 7 |
computers unlimited |
10 |
| 8 |
wiblog |
8 |
| 9 |
javascript zoom |
15 |
| 10 |
cartoon children |
106 |
All in all a pretty fair reflection of what I do with my time, I think you’ll agree.
Sun Jul 23
I’ve been pretty much offline for the last 3 or 4 days and it’s given me chance to catch up with some neglected household chores. Like cleaning, gardening, spending time with my new son, that sort of thing. It’s back to work as usual tomorrow, and armed with a freshly-written (and very long) to-do list I have high hopes of knowing exactly what I need to do to make progress with my various projects. Notice I said “knowing what to do” and not “getting it done”.
However, I have a couple of new things I’ll be telling you about shortly, so hopefully that disclosure of my current projects will help to make me motivated enough to get things done. And, who knows, hopefully finished.
Wed Jul 19
Today I had the chance to mess around a bit more with the marvellous Prototype Javascript framework which in my opinion just keeps getting better and better. It makes me wish I had started using it 18 months ago when I first started getting (back) into Javascript and this thing called AJAX, it would have saved me a lot of head - and heart - ache. I’d highly recommend it.
However there’s one problem. And that’s tied to the fact that the vast majority of work I do is with databases, and therefore that means recordsets. A recordset, in case you didn’t know, is just a collection of records from a database. Say, a list of the details of all the users with a security level of ‘Administrator’. So, of course, I’m looping those records in PHP or ASP and outputting the result. So far so good. But what if I want to attach Javascript functions to each record, for example toggling a ‘more details’ section?
What I’d traditionally have to do is find out the records I’m going to be printing out, set up some kind of listener for each one at the top of the page, then print the recordset further down. That means two loops. Let me explain, in psuedo-code:
// Get all the users with a type of 'Administrator'
$recordset = $database->getRecordset(”All users who are Administrators”);
// Loop all the records
for each $user in $recordset {
// print each users name
print $user[”name”];
// print a hidden area with further details on this user
print “”;
// print a few more details about the user
print $user[”email”]
print $user[”phone”]
// end the hidden area
print “”;
What I want to do is place a Javascript listener for each username so when it is clicked the hidden area is shown for that area. Of course, using Prototype and all the other Javascript frameworks I’ve seen, you have to add a separate listener for each thing you want to listen for. That makes sense.
However the difficulty is that I need to know about all the usernames at the top of the pge, where I can create my Javascript and put it in the head section, but often I don’t open the database until further down. Yes, I know, I know, doing it all as objects sorts this problem out, but that’s not always practical for older projects.
What I need is a method in Prototype that will allow me to listen for elements with attributes like what I want. In my example (using real code this time) I could create the following using my server-side code:
User 1
user1@email.com, 11111</p>
User 2
user2@email.com, 22222</p>
User 3
user3@email.com, 33333</p>
Then I would set my Javascript code to look for all elements with and ID like ‘user-[whatever]’ and set a listener for each element it finds. Like this:
// Loop all elements with ID like 'user-' then any series of numbers, using regular expression syntax
for (var i=0; i
// get the value from the ([0-9]*) section for this element
var thisId = document.getElementsByIdLike(’user-([0-9]*)’)[i][0];
// for each element found set a listener using Prototype
Event.observe(document.getElementsByIdLike(’user-([0-9]*)’)[i].id, ‘click’, toggleDetails, false);
}
It’s a bit rough-and-ready (what else do you expect from me?) but I think this function would be really useful. Does anyone else agree? Does anyone else get what I’m on about?
The other thing I’ve been using today is JSON, a really lightweight way of transferring data about. It’s as easy as pie to parse it using Javascript, and makes a big step forward from my usual blunderbuss method of returning chunks of HTML from AJAX calls. Maybe this goes to prove I’m moving on in my geekdom. I’ll leave it up to you to decide whether that’s a good thing.
Tue Jul 18
Chris Anderson, erstwhile editor of Wired magazine, member of the technological elite and author recounts a story about early 80’s hair, a battle of bands, and how he was nearly in REM.
Fantastic, I’d have loved to be there. Funnily enough just the other day I dug out my copy of Green by REM (disclaimer, I get a few pennies if you buy from that link) which may or may not be their best album, but I enjoyed it immensely anyway.