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: documents, downloadable articles, HTML, javascript, offline, tutorials, versioncontrol, webdevtrick


January 14th, 2008 at 3:17 pm
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.
January 14th, 2008 at 3:24 pm
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.
January 14th, 2008 at 3:48 pm
Neat idea! Very smart.
January 14th, 2008 at 4:05 pm
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!!
January 14th, 2008 at 4:46 pm
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 :)
January 14th, 2008 at 5:21 pm
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…
January 14th, 2008 at 5:27 pm
@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 :)
January 14th, 2008 at 11:50 pm
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 :)
January 21st, 2008 at 4:46 pm
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) ?