donderdag 9 juni 2011

LowerCase your links with JQuery

A friend recently asked me and some other friends in a chatroom to come up with a way to set href attribute values of all the links to lower-case. Don't really know why he needed it exactly but anyway, here is the solution I came up with.

Put a JavaScript on the webpage with this code
$(document).ready(function(){
//read in the document, this is important

$("a").each(function(index){
//now for each link, take the href make it //lowercase and put it back
$(this).attr('href', ($(this).attr('href').toLowerCase()));


});

});
The big downside here is that it depends on JQuery, you'll have to import a JQuery on that page to. So while this is a really quick fix, this took me like 5 minutes to come up with type in and put on-line, importing the whole JQuery library for this lousy toLowerCase() function isn't really that elegant.

In the end, it'll probably all be done by some Perl script that goes through all the pages and fixes all the html all at once.