Start semantics simply

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

You may or may not have heard of microformats. They’re basically along the lines of the semantic web project but in a more practical way that is more likely to take off. It basically involves adding extra bits of semantics to web pages (or any XML-based documents) in a consistent way that doesn’t limit what already exists. But there are much simpler good practices that need to be encouraged regarding semantic markup first.

This blog post illustrates a wonderful misuse of HTML and offers an important explanation about why it is a misuse (or at least a decidely-less-useful use).

Making progress in German

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

I’m now officially one ninth of my way through the Pimsleur German course (that is I’ve finished Unit 10 of German I - Beginner). It’s still as good as I said previously. Although I think perhaps they should rename “How to be annoying in German”. Towards the end of unit 10, you have a conversation with a woman about having a meal.

“She will suggest a series of times to you. Refuse each time in turn and suggest an hour later.”

Another funny thing I noticed looking at the covers for the rest of the course, the three levels are “Beginner”, “Intermediate” but not “Advanced”. The third level is “Intermediate Plus”. Subtle way of saying even if you complete the course you have a way to go yet.

Least favourite German fragment so far: “jetzt nichts”.

Bad DOM

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

The Dom Scripting Taskforce says adding non-standard markup to page using Javascript is OK. Well I wouldn’t call it a “best practice” but from a personal point of view I can’t see a practical disadvantage.

For me the problems with bad markup come from trying to parse it automatically (using PHP’s SimpleXML object for instance). Since the javascript will basically only affect the page a human sees and not a machine, many of the disadvantages of non-standard markup fade away.

How I wrote the list thingy in Javascript

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

It is assumed you know basic Javascript before reading this and know what the DOM is.

First the source code:

function setUp() {
    var myULs = document.getElementById('tree').getElementsByTagName('ul');
    for (var i = 1; i < myULs.length; i++) {
        var toggleObject = document.createElement('a');
        toggleObject.innerHTML = '+ ';
        myULs\[i\].parentNode.insertBefore(toggleObject, myULs\[i\].parentNode.firstChild);
        toggleObject.onclick = eventCaller(toggleObject, myULs\[i\]);
    }
}

function eventCaller(toggleButton, list) {
    list.style.display = 'none';
    return function() {
        if (list.style.display == 'none') {
            list.style.display = '';
            toggleButton.innerHTML = '- ';
        } else {
            list.style.display = 'none';
            toggleButton.innerHTML = '+ ';
        }
    }
}

onload = setUp;

First, how function setUp works.

var myULs = document.getElementById('tree').getElementsByTagName('ul');

To use the code you need an element with the id of “tree” surrounding the list you want it to work. This code just gets a collection of ul tags with in the id="tree" tag. This collection is looped through using a for loop.

var toggleObject = document.createElement('a');
toggleObject.innerHTML = '+ ';

This creates a new anchor a element and sets the text inside to a plus sign. I probably shouldn’t use innerHTML but it makes things simpler.

myULs[i].parentNode.insertBefore(toggleObject, myULs[i].parentNode.firstChild);

This looks messy but is really simple. _someNode_.insertBefore adds a node to _someNode_ before some child of _someNode_. In this case we want our newly created anchor tag to be the first child of the ul’s parent. (i.e. place the link before the ul and before any text as well).

toggleObject.onclick = eventCaller(toggleObject, myULs[i]);

The onclick event is then set to some function. It is important to realise the onclick is not set to eventCaller but the function eventCaller returns.

function eventCaller(toggleButton, list)

The function is called with two arguments: a list and the link before it that will toggle the display of the list.

list.style.display = 'none';

First we just set the list to not display by default.

Then the clever bit, we return a function.

What the function does is simple, it toggles the display style attribute of the list and changes the “button” between plus and minus signs.

But the function is different for each list. Because the function is declared within another function, this new anonymous function still has the scope of the function that created it. This means each copy of this new function has different list and toggleButton variables.

Cunning closures

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

I just wrote a nifty bit of Javascript to allow nested lists to be displayed as a tree with little buttons to hide and show parts. It’s incredibly simple to use.

Firstly include the javascript file with a script tag (obviously). Then put a span element around the list you want to be undisplayable that has the class tree (in fact it can be any element with the correct class).

The clever bit (for me at least) is that it uses a closure to do this. Explaining what a closure is is difficult to do quickly but it involves a function returning a function that is altered by the values passed to the function (the first one). It’s being used right now in my sidebar to display “pages”.

The power of coincidence

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

In March I spent a weekend in Blackpool with my girlfriend, mainly to go to Blackpool Pleasure Beach, a theme park. While we were there we briefly met a guy trying to sell us a CD of “monk rock” or something (one of those people collecting for a good cause basically.

Well yesterday we met him again in Rotherham. Same person doing the same thing. Quite how unlikely it is that one of us could meet the same guy twice is amazing, but we spend a lot of time apart so to think we could both meet him together is even more amazing.

I think I’ve found my traffic

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

Have you heard of hReview? It’s a microformat aimed at marking up reviews of things in a consistent way with web pages (or any XML based document). The wiki discussing it links to implementations and SiteReviews.org implements it and so gets a link.

Whoopee!

PHP Security Checker

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

Here’s an interesting utility (that most WordPress users will have seen already since it’s mentioned on the news part of the dashboard: a PHP Security Checker.

I’m not sure how it works; it sounds like it just scans PHP scripts and returns references to possibly dodgy bits of code. Donncha O Caoimh of Holy Shmoly! raised a point that I never really thought about - the security of WordPress themes. However much the creators of WordPress like to point out that their template files don’t require knowledge of PHP, that doesn’t mean you can’t put PHP in them to do whatever you like… Maybe this utility will help.

Let there be colour

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

I decided if Ryan is going to post more often there needs to be an easier way to tell our posts apart, so I decided to fiddle around with the theme a bit. Basically all I did was put a div tag around all the posts setting the class to the author of the post and then added style information for .Ryan and .Oliver. I also changed the top header slightly too.

OrbWars Has Closed!

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

Well Oliver mentioned this briefly below, but I thought I’d just point it out again, as I just realised you can’t post comments to Pages, only Posts!!! So now at least anyone wanting to comment can do so! Anyway, check this out for details: http://www.oliverbrown.me.uk/orbwars-closed/