A quick idea: JavaScript version controlling for static HTML documents

When you write tutorials and you want people to use them wherever they are it is a good idea to offer the HTML documents as a zip for downloading. The benefit to the end user is that they don’t need to be online to look something up (I for example have the HTML 4.01 documents on my machine as HTML documents). The drawback is that the documents could be outdated without the user knowing – even when they are online while watching them.

Now, I pondered a bit about this and wondered if something like this wouldn’t be a solution:

  • You add a version number to the title of each document.
  • You add a remotely hosted versions.js script at the end of each document.
  • This script has a JSON object with the version information of each document and compares the file name and version.
  • If the version is outdated, it generates an error message that gets shown to the user.

You can try it out by downloading the two demo documents un-zip them and open them on a computer that is connected to the internet. The second document linked from the first one should be outdated.

The source of versions.js


(checkversion = function(){
  // change as fit
  var versions = {
    'documentexample.html':'1.0',
    'documentexample2.html':'2.0'
  };
  var errorID = 'versionerror';
  var errorMessage = 'This document is outdated, please go to the homepage to download the new version!';

  // checking code
  var d = document;
  // get the version number
  var cv = d.title.match(/\(version (.*)\)$/); 
  // get the file name
  var cn = window.location.href.split('/'); 
  cn = cn[cn.length-1].split('#')[0];
  // check and create error message if there is a mismatch
  if(cv[1] && versions[cn]){
    if(versions[cn] !== cv[1]){
      if(!d.getElementById(errorID)){
        var m = d.createElement('div');
        m.id = errorID;
        m.appendChild(d.createTextNode(errorMessage));
        d.body.insertBefore(m,d.body.firstChild);
      }
    }
  }
}());

You could create both the titles and the JSON object in versions.js on the backend automatically by scanning the titles or from a version control system. What do you think?

Tags: , , , , , , ,

9 Responses to “A quick idea: JavaScript version controlling for static HTML documents”

  1. Nicolas Le Gall Says:

    I think this is a great idea :)
    But, is there a solution to directly link to the new version ?
    This could avoid searching for the page, tutorial, etc.

  2. Chris Says:

    Good point, you could provide a baseURL in versions.js and create the link in the error message. This gets a bit tricky though when dealing with different sub-folders.

  3. Ara Pehlivanian Says:

    Neat idea! Very smart.

  4. Marc Says:

    Good idea! How about a php script (or some other language) that would read the filedate on the script.js and spits that back to the client as JSON. It could also simply read the contents of the script.js and overwrite a javascript date var…

    Not sure how (in details) yet but this could turn out to be very usefull!!

  5. RobMcM Says:

    This is a great idea, yet so simple.

    The only problem is, people may be looking at this offline…

    You could have an option to load in the new version via the DOM if it’s available. The problem then is when they are next off line they would get the old version again. However once you detect there is a new version, you could set a cookie to alert them it’s out of date when they are next off line.

    Or you could tell them to download a new version if it’s out of date, but then you have problems with people organizing things etc… The DOM update solution would solve that but then lack for offline compatibility.

    Perhaps Google Gears would be the total solution :)

  6. Stuart Langridge Says:

    Correct me if I’m wrong here, but this isn’t that helpful: it only works at the points when you don’t need it because you’re connected to the net anyway. If you’ve got a net connection then use the online documents; if you don’t have an online connection then it won’t work…

  7. Chris Says:

    @sil, not really and yes maybe. Call it an alert. If you use the documents offline you won’t get a difference, if you open them from your HD and go online you’ll get an alert to get the new ones. Much like any virus software or Mac/Windows update works, too. With you mainly using communist operating systems you wouldn’t know of course :)

  8. sil Says:

    Chris: I see the theory; it’s just not something that can be relied upon. Nice idea, though.

    And…we’ve had automatic updates since, ooh, forever, dude. Your proprietary vendor-lockin operating systems are just starting to catch up to where Debian was ten years ago :)

  9. gillesB Says:

    An improvement could be to provide a way to update your copy if it is updated, like a ’synchronize’ button.

    Maybe Harmony could be used (http://alliance.seas.upenn.edu/~harmony/cgi-bin/demo.php) ?

Leave a Reply

Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).