Load external <script> without cache using Javascript

javascript, jquery

Solution

Use jQuery's getScript instead of a script tag.

$.getScript("http://example.com/cool.js");

or pure JavaScript

var scr = document.createElement("script");
scr.src = "http://example.com/cool.js" + "?ts=" + new Date().getTime();
document.getElementsByTagName("head")[0].appendChild(scr);

Problem

I want to load an external javascript file into the page and make sure its not cached. I do not have access to php so I cant generate a random string after the filename. In PHP the script would look like this: ``` <script src="http://site.com/cool.js?<?php echo $randomnumber; ?>"></script> ``` Is there a way to do something like that using only javascript?

Original source

Related problems