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.

2 opmerkingen:

  1. The reason was case-sensitivity. I was running an Apache server on Windows and moved it to Solaris. A lot of links didn't work because of characters with a different case. To rectify the situation, I needed to convert filenames and href tags to lowercase.

    For the content of the files, this is the perl regex that can be used:
    1) slurp the file to $content
    2) $content =~ s/href="[^#"]+/lc ($&)/ge;
    3) burp the $content back to the file

    BeantwoordenVerwijderen
  2. I really should start to learn how to read regex I guess.

    BeantwoordenVerwijderen