Browser history sniffing with Dojo

Posted by Mike on Jan 14th, 2010

Niall Kennedy posted a now-famous article about using some browser trickery to determine what websites a user on your site has visited. I’ve taken that concept and created a module that can be used with the Dojo Toolkit javascript framework.

It provides two methods that you can use in your code, isVisited and isAnyVisited.

One important note about your URL specification is that you must specify an *exact* URL that the user has been to. So, for example, if someone has visited several twitter profiles, but never actually went to the homepage or signin screen, then it would be difficult/impossible to tell whether or not they were a twitter user without a massive brute-force query.

You can download sniff.js, or view a demo.

Sample usage:

dojo.require("mdg.sniff");
var usedYahoo = mdg.sniff.isVisited("http://www.yahoo.com");
var usedGoogleMaps = mdg.sniff.isAnyVisited([
      "http://maps.google.com",
      "http://maps.google.com/maps"]);
var usedFacebook = mdg.sniff.isAnyVisited([
      "http://www.facebook.com",
      "http://www.facebook.com/index.php",
      "https://login.facebook.com/login.php"]);

Source code:

dojo.provide("mdg.sniff");
 
//
// Browser history sniffing, based on infamous blog post:
// <http://www.niallkennedy.com/blog/2008/02/browser-history-sniff.html>
//
// Sample usage:
//
//    dojo.require("mdg.sniff");
//    var usedYahoo = mdg.sniff.isVisited("http://www.yahoo.com");
//    var usedGoogleMaps = mdg.sniff.isAnyVisited([
//          "http://maps.google.com",
//          "http://maps.google.com/maps"]);
//    var usedFacebook = mdg.sniff.isAnyVisited([
//          "http://www.facebook.com",
//          "http://www.facebook.com/index.php",
//          "https://login.facebook.com/login.php"]);
//
// Works with Dojo 1.3 and 1.4 (*may* work with 1.2 as well)
//
 
dojo.require("dojox.html.styles");
 
(function(){
 
    var _this = this;
 
    this.sniffCache = {};
 
    dojox.html.insertCssRule(".dojohistorysniff a", "color:#000000;");
    dojox.html.insertCssRule(".dojohistorysniff a:visited", "color:#ff0000 !important;");
 
    this.isAnyVisited = function(/*Array*/urls) {
        for (i=0; i<urls.length; i++) {
            if (_this.isVisited(urls[i])) {
                return true;
            }
        }
        return false;
    };
 
    this.isVisited = function(/*String*/url) {
        if (typeof(_this.sniffCache[url]) != 'undefined') {
            return _this.sniffCache[url];
        }
        var link = _this.addLink(url);
        var color = new dojo.Color(dojo.style(link, "color"));
        if (color.r == 255) {
            _this.sniffCache[url] = true;
            return true;
        }
        _this.sniffCache[url] = false;
        return false;
    };
 
    this.insertSniffDiv = function() {
        return dojo.create("div", {className: "dojohistorysniff"}, dojo.body());
    };
 
    this.getSniffDiv = function() {
        var divs = dojo.query("div.dojohistorysniff");
        if (divs.length > 0) {
            return divs[0];
        }
        return _this.insertSniffDiv();
    };
 
    this.addLink = function(/*String*/url) {
        var div = _this.getSniffDiv();
        return dojo.create("a", {href: url}, div);
    };
 
    /**
     * mdg.sniff.isVisited
     * Check whether or not a URL has been visited
     * @param url String
     * @return boolean
     */
    mdg.sniff.isVisited = this.isVisited;
 
    /**
     * mdg.sniff.isAnyVisited
     * Check whether or not *any* of the URLs specified have been visited
     * @param urls Array of Strings
     * @return boolean
     */
    mdg.sniff.isAnyVisited = this.isAnyVisited;
 
})();
  • Twitter
  • Facebook
  • StumbleUpon
  • Google Reader
  • Reddit
  • Share/Bookmark

13 Responses

  1. Gary Says:

    Will definitely be trialling the script as above. Thanks for sharing Mike.

  2. Alex Says:

    This does not work in Safari 5.0.

  3. Jraz Says:

    Just following up to Alex’s post, how come this works on most browsers but not Safari?

  4. GogeoffeloJeK Says:

    faxless payday loansfaxless payday loans , http://quickfaxlesspaydayloanonline.com/#15222 payday loan online

  5. lemsfoems Says:

    generique viagraviagra en ligne , http://acheterviagraenlignefr.com/#15830 acheter viagra en ligne

  6. Roahalgop Says:

    Shelf Life Levitra Online Pharmacy Viagra On Prescription In The University Of Kentucky Celecoxib Generic Medications Discount Pharmacy Cialis Viagra On Nhs Prescription Public Prescription Services Soma Methotrexate In Psoriasis Liver Damage Prozac Clinical Trial Obsessive Compulsive Disorder How Long To Get Hooked On Prozac Atarax Pfizer Pregnant Mouse Soma No Rx Overnight Delivery buy adipex without rx How Do Ativan Make You Feel Viagra Negative Risks Xanax Patient Information Ultram Pain Releiver buy klonopin online cheap Furosemide Javma Singulair Q Var Amoxicillin And Advil Together Amoxicillin Trihydrate Depression No Prescription Buy Cheap Soma Cialis In Usa Discount Soma 375 Mg Soma Delivery Culture Amoxicillin Dose Dogs Buy Singulair No Prescription Shipping Uses Of Alprazolam Borderline Personality Disorder Lorazepam R59 Round White Pill Soma Drug Risks Ultram Er Police Viagra And Extacy Cialis Levitra

  7. Pharmf224 Says:

    Hello! dkdbaea interesting dkdbaea site! I’m really like it! Very, very dkdbaea good!

  8. Buy online ativan Says:

    cppgcnjlf.hsjggjui, Buy ativan chat room, aCEPysA, [url=http://www.bigboysandtheirtoys.com/ativan.html]Buy ativan online 32[/url], eSVtpNM, http://www.bigboysandtheirtoys.com/ativan.html Where to buy ativan, WkjtJGl.

  9. Kirtanuls Says:

    +- levitra no prescription
    levitra online no prescription
    http://buylevitraonlinetoday.com/#79800 — generic levitra no prescription

  10. What is the adverse reaction of ativan Says:

    pbeknnjlf.hsjggjui, How much ativan used to make someone comotose, oEevLGf, [url=http://ativanknowledgebase.com/]How long is ativan in the body[/url], oemFtot, http://ativanknowledgebase.com/ How much ativan used to make someone comotose, nPRCWbd.

  11. Stendra Says:

    topcunjlf.hsjggjui, Cialis levitra staxyn stendra and viagra, DIlNyMF, [url=http://stendra365.com/]Stendra[/url], JcWWScs, http://stendra365.com/ Stendra, EKGKuZo.

  12. Sandy Says:

    Another type of comparison you could want to create is if the
    cash advance lender allows extensions or renewals and how often times they permit you to accomplish this.
    These options are not meant to get long-term solutions because their interest levels less difficult higher along with the loan amounts are
    normally under one thousand dollars.

  13. free coupon code Says:

    Having read this I believed it was rather enlightening.
    I appreciate you taking the time and energy to put this article together.
    I once again find myself spending a significant amount of time both reading and commenting.
    But so what, it was still worthwhile!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.