Created
August 18, 2012 03:24
-
-
Save pfeilbr/3384155 to your computer and use it in GitHub Desktop.
Reload Page if [Java]Script Changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: depends on jQuery and hex_md5 function @ http://pajhome.org.uk/crypt/md5/ | |
function reloadPageIfScriptChanges(patterns, pollInterval) { | |
patterns = $.isArray(patterns) ? patterns : [patterns]; | |
$.ajaxSetup({ cache: false }); | |
var digests = {}; | |
setInterval(function() { | |
$('script[src]').each(function() { | |
var src = $(this).attr('src'); | |
if( $.grep(patterns, function(p) { | |
return p.test(src); | |
}).length == 0) { | |
return; | |
} | |
$.get(src, function(data) { | |
console.log('checking ' + src); | |
var md5 = hex_md5(data); | |
if ( (typeof digests[src] === 'undefined') ) { | |
digests[src] = md5; | |
console.log('assign'); | |
} else if (digests[src] !== md5) { | |
console.log(src + ' changed. reloading page'); | |
location.href = location.href; | |
} | |
}); | |
$('body').html(mydata.name); | |
}); | |
}, pollInterval); | |
} | |
reloadPageIfScriptChanges(/data\.js/, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment