no more cache – force the reload [ 743 views ]
Goal: to avoid the cache provided old file versions
If I load an external javascript or css file from js code like this:
// load a css file var s = document.createElement('link'); s.setAttribute('rel', 'stylesheet'); s.setAttribute('type', 'text/css'); s.setAttribute('href', 'main.css'); document.getElementsByTagName('head')[0].appendChild(s); // load a js file var s = document.createElement('script'); s.type = 'text/javascript'; s.src = 'main.js'; document.getElementsByTagName('head')[0].appendChild(s);
First time is ok, but second time may be I will receive the file from the cache.
To avoid the cache just put some fake parameter to the end of the file request.
... s.setAttribute('href', 'main.css?_' + new Date().getTime()); ... ... script.src = 'main.js?_' + new Date().getTime(); ...
In this case the client will ask the server every time and never the cache.