Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created March 15, 2012 18:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/2045904 to your computer and use it in GitHub Desktop.
Save jonathantneal/2045904 to your computer and use it in GitHub Desktop.
CSS Refresh (as seen @ http://cssrefresh.frebsite.nl/)
function refreshLink(link) {
var instance = this;
var pageDomain = window.location.href.match(/\/\/([^\/]+)/);
var linkDomain = link.href.match(/\/\/([^\/]+)/);
if (!pageDomain || !linkDomain || pageDomain[1] != linkDomain[1]) {
return instance;
}
instance.link = link;
instance.href = link.href;
instance.refresh = 200;
instance.timestamp = +new Date;
var request = window.ActiveXObject ? new ActiveXObject( 'Microsoft.XMLHTTP' ) : new XMLHttpRequest();
var timeout = function () {
request.open('HEAD', instance.href, true);
request.send(null);
};
request.onreadystatechange = function (e) {
if (request.readyState == 4) {
var timestamp = +new Date((request.getAllResponseHeaders().match(/Last-Modified:([^\n]+)/) || ['', ''])[1]);
if (instance.timestamp < timestamp) {
instance.href = instance.href.replace(/#.+/, '') + '#' + timestamp;
instance.link.href = instance.href;
instance.timestamp = timestamp;
}
setTimeout(timeout, instance.refresh);
}
};
setTimeout(timeout, instance.refresh);
return instance;
}
function refreshLinks() {
var instance = this;
instance.refresh = 200;
var timeout = function () {
for (var a = document.getElementsByTagName('link'), i = 0, l = a.length, e; i < l; ++i) {
e = a[i];
if (!e['data-refresh']) {
e['data-refresh'] = new refreshLink(e);
}
}
setTimeout(timeout, instance.refresh);
};
setTimeout(timeout, instance.refresh);
return instance;
}
@jonathantneal
Copy link
Author

Usage: new refreshLinks()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment