I am using the following code to dynamically add scripts in JavaScript:
function loadJS(file, c, preventCache)
{
var jsEl = document.createElement("script");
if (typeof preventCache === 'undefined' || !preventCache)
jsEl.src = file;
else
jsEl.src = file + "?" + Math.floor((Math.random() * 10000));
if (c) { jsEl.addEventListener('load', function (e) { c(null, e); }, false); }
document.getElementsByTagName("head")[0].appendChild(jsEl);
}
Appending "?" + Math.floor((Math.random() * 10000))
disables caching altogether. What I would like to do instead, is append a time stamp of the last modified time of the file. Then the file can be cached but only when it is up to date. How can I efficiently get the last modified time of the file?