Make $.getScript("") not add numbers at the end of the request

748 views Asked by At

I am using $.getScript() to get a script from somewhere.

function fetch(url){
window.setInterval(function (){
$.getScript(url);
},50000)
}

fetch("http://example.com/script.js");

However, when I look at the FireBug developer console, I have seen that it adds additional ?numbers. Here is the output:

<script async="" src="http://example.com/script.js?7330519448833367000&_=1416681336440">

The remote server has disabled the ? char from their htaccess. I need to use $.getScript("") to get the remote script, but how is it possible to prevent the function that adding additional ?numbers ?

2

There are 2 answers

2
SameOldNick On BEST ANSWER

It's because jQuery sets caching to false (by default) when you use $.getScript().

From the jQuery documentation:

By default, $.getScript() sets the cache setting to false. This appends a timestamped query parameter to the request URL to ensure that the browser downloads the script each time it is requested.

To disable caching, add the following before the $.getScript call:

$.ajaxSetup({
    cache: true
});
0
pstenstrm On

$.getScript() tries to bypass the cache to always download the latest files. The numbers are a timestamp.

To disable this you need to set this propery:

$.ajaxSetup({
    cache: true
});