Append Dynamic version(variable ) in Script tag and stylesheet based on time
If you are looking for the shortest solution, how about this?
<script>document.write('<link href="/assets/cder.css?v=' + Date.now() + '" rel="stylesheet" />');</script>
A worthy alternative should be:
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/assets/cder.css?v=' + Date.now();
document.body.appendChild(link);
</script>
Well, you must escape the closing script tag as follows:
<script>document.write('<script src="/assets/abc.js?v=' + Date.now() + '"><\/script>');</script>
An example of how to add several scripts:
<script>
var scripts = [
'/assets/abc.js',
'/assets/def.js',
];
for (var i = 0; i < scripts.length; i++) {
var script = document.createElement('script');
script.onerror = function() {
alert('Could not load ' + this.src);
};
script.src = scripts[i] + '?v=' + Date.now();
document.body.appendChild(script);
}
</script>
You could do this dynamically from javascript.
<script>
var my_awesome_script = document.createElement('script');
my_awesome_script.setAttribute('src', '/assets/abc.js?v='+new Date.getTime()); //was missing single quote
document.head.appendChild(my_awesome_script);
</script>
taken from Stack overflow answer