Google Map UK Postcodes Bookmarklet

Today I found a really neat use for my Get Elements By Content function which I wrote recently.  I wanted to create a bookmarklet that allowed me to visit a page, click on the link and turn any UK postcodes in the page to a link to a Google Map.  Well, it’s pretty simple –

var pc=gbc('[a-z]{1,2}\\d{1,2}[a-z]? \\d[a-z]{2}');
for(var i=0;i<pc.length;i++){
 pc[i].innerHTML=pc[i].innerHTML.replace(/([a-z]{1,2}\d{1,2}[a-z]? \d[a-z]{2})/ig,'<a href="http://maps.google.co.uk/maps?f=q&hl=en&q=$1,UK&ie=UTF8&z=16&om=1&iwloc=addr">$1</a>');
}

Obviously you need to ‘gbc’ function from my last post, but other than that, you just need to turn it into a bookmarklet and away you go. If you have any problems, let me know.

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live

Advertisements

Javascript: GetElementsByContent

I had the inclination last week to knock up a quick Javascript function to effectively give you GetElementsByContent. So, you can call a function which returns an array of nodes which match a string (or RegExp) which you pass in, which can optionally be filtered by tag name.

Now, I make no claims that this is “a first”, as it probably isn’t, I wanted to write it, so I did.  If I’d have googled it, I’d have probably found something really quickly that did exactly what I wanted and would probably be far superior to what I have written.  I wanted to write it for fun really, but I think it’s actually quite useful, so here it is –

var gbc=function(contents,tag){
  var n=[];var s=tag||'*';
  var b=document.getElementsByTagName(s);
  var r=new RegExp(contents,'ig');
  for(var i=0;i<b.length;i++){
    for(var t=0;t<b[i].childNodes.length;t++){
      if(b[i].childNodes[t].nodeType==3&&r.test(b[i].childNodes[t].textContent)){
        n.push(b[i]);
        break;
      }
    }
  }
  return n;
}

Because I’m lazy I’ve called it ‘gbc’ (Get By Content), but obviously you can name it whatever you like.

You use it like –

var f = gbc('forums');

This would return all nodes in the current document which contain the text ‘forums’.

You could do –

var f = gbc('forums','a');

This would return all nodes matching the text ‘forums’ (within the text content, not attributes), but limit the search to just ‘a’ tags.

Because the first parameter (the text to search) is actually treated as a regex, you can actually pass it a string to treat as a regex. So –

var f = gbc('abc\\d+');

This would return all nodes which contain the text ‘abc’ followed by one or more numbers. Note the double slash, this is due to the way Javascript interprets the RegExp when using the ‘new’ style syntax, you need to escape any slashes.

That’s about it really, let me know if you use it or have any feedback.

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live

Switched Theme

Unfortunately, I’ve had to move away from my old theme, which is quite sad as Dennis featured so prominently on the old one. I’ll probably have to go for the CSS upgrade and get him back in here that way.

I needed to switch in order to get away from fixed width, which was ruining any code I pasted. As I intend to start posting more code, the old them had to go.

Right, now I need to right some new posts..