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

你离开我真会死。 提交于 2019-12-11 04:39:20

问题


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 ?


回答1:


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
});



回答2:


$.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
});


来源:https://stackoverflow.com/questions/27081052/make-getscript-not-add-numbers-at-the-end-of-the-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!