The blagotube home of Sune Kirkeby. Here are rambling rants and some bits of code.

19. Apr
2005

Next/previous links and gmail-style keyboard navigation

I’ve now added rel='next' and rel='previous'-links and visible next/previous links to the blog-post pages. Also, I added gmail-style keyboard-navigation to the entire site. It works for 'u' for Up, 'j' for Previous and 'k' for Next. It was pretty simple to implement:

document.onkeydown = function(event) {
    if(!event) var event = window.event;
    if(event.keyCode == 85 /* 'u' */) {
        document.location = '..';
    } else if(event.keyCode == 74 /* 'j' */) {
        var e = find_link_rel('previous');
        if(e) document.location = e.href;
    } else if(event.keyCode == 75 /* 'k' */) {
        var e = find_link_rel('next');
        if(e) document.location = e.href;
    }
};
function find_link_rel(rel)
{
    var children = document.getElementsByTagName('link');
    var i, child;
    for(i=0; i<children.length; ++i, child=children.item(i))
        if(child && child.tagName == 'link' && child.rel == rel)
            return child;
}
This post was written by Sune Kirkeby on 2005-04-19, and claimed to be mostly about rambling.