29th June 2008

Getting browser history using Javascript

Something we discussed recently at work was using Javascript to obtain the browser history of a user visiting your website. It was deemed a little black-hat but I'm not afraid of such things, so I set about writing a quick script to sniff a user's history.

Why

I read a rumour on the Internet recently that Dell were using such a script to work out whether you've visited competitor websites; allowing them to put their best offers under your nose in an aim to get your business. So under the premise that you've got a website with known competitors, how do you know if your visitor has visited them? That's where my little script would come in handy.

What it does

You enter the URL's of your competitor's websites into a Javascript array and set a colour for visited links in the CSS. Then it's just a case of checking the colour of the A-tags on the page; if you've visited them I'll know about it. So let's start looking at the code...

XHTML

All I've done is create a really basic page that just has a container and a div with the ID of 'sniffer'. This is the div that I'll use for calculating whether various places have been visited. So the code is really basic here:

Sniffer is empty for now and I've just placed some instructions on the page in case you've not visited any of my test URLs.

CSS

The crucial thing to do is ensure that you're setting a color for both a:link and a:visited within the 'sniffer' div, this will ensure we can perform colour checks on them later. So all I've got is:

So for regular links I'm telling them to be blue, whereas visited links are going to be red. Note that I've commented out the layout CSS for the #sniffer div; this would position it off-page so that visitors to the page wouldn't be able to see the list I'm checking. However I've commented it out so that (for demonstrative purposes) you can see what I'm checking.

Javascript

I'm using Mootools (yet again, no surprises there) for this, although you could do this with straight up prototype (or conventional Javascript probably). The framework doesn't matter too much, I'm just using Mootools because I have it handy and I know the selectors off the top of my head. Let's look at the script:

All I'm doing here is setting up the array of competitors; it can be as many items as you like but in this instance I've used 4 common URLs. I'm then creating an array that will store which links you've actually visited, as well as setting the colour I'm looking for. This colour is the one that we defined in the CSS earlier, note that the hexadecimal characters are lower case, this is important because of how Javascript reads the colour codes for items; it won't match if you leave it uppercase (not without doing a conversion anyway).

As usual I'm initialising my JS on domready. I'm setting the sniffer container and creating an unordered list HTML element, which I then inject inside of the 'sniffer' div (there's no real need to do this, I just like using UL's to keep my work tidy). I'm then iterating through the competitors array and creating a new list item for each one, with a link to that URL (which are both then injected inside of the UL). I'm then setting the innerHTML of the element so that we can see something on the page (again, no real need to do this because realistically you're not going to show this to the user).

All I'm doing after that is using the Mootools element.getStyle() function to obtain the 'color' attribute for the A-tag I've just injected. If the link colour matches the hexadecimal item set in the 'visitedLink' variable, then you've visited this URL. All I'm doing at this point is alerting that information out (so you can see it's working) and adding it to the array of visited places.

Done! Nice, quick and easy but it could come in useful for increasing conversions on your website. It's a little underhand (read unethical) but that's business for you.

Tagged with javascript