javascript 在 URL 后添加一个随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7432361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:06:54  来源:igfitidea点击:

Adding a random number after the URL

javascriptjquery

提问by Mausoleum

I've got a site which uses jQuery and Ajax to change the site content without reloading the page. The site contains content which I often change. But somehow the page gets saved in the cache so it doesnt show the changes.

我有一个站点,它使用 jQuery 和 Ajax 来更改站点内容而无需重新加载页面。该网站包含我经常更改的内容。但不知何故页面被保存在缓存中,因此它不显示更改。

I tried several things to make the browser not to save the site into the cache like METAs and PHP. But it doesnt work.

我尝试了几种方法来使浏览器不要像 META 和 PHP 那样将站点保存到缓存中。但它不起作用。

I think it has to do with the fact, that the page always has the same URL so I thought about adding a random number to it like:

我认为这与页面始终具有相同的 URL 的事实有关,所以我想向它添加一个随机数,例如:

window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);

(It's not my code, found it with some googlin) But this only adds the link ID I clicked on to the URL. I don't know where to put "Math.random()" to add a random number.

(这不是我的代码,是用一些谷歌搜索找到的)但这只会将我点击的链接 ID 添加到 URL。我不知道把“Math.random()”放在哪里来添加一​​个随机数。

Hope you can help!

希望你能帮上忙!

回答by Sahil Muthoo

Just use cache : false. jQuery will automatically add a timestamp to the end of the URL for you, making sure that ajax requests are never cached.

只需使用cache : false. jQuery 会自动为您在 URL 末尾添加时间戳,确保永远不会缓存 ajax 请求。

Quoting the the API reference for .ajax()

引用 API 参考 .ajax()

cache Default: true, false for dataType 'script' and 'jsonp'

If set to false, it will force requested pages not to be cached by the browser. Setting cache to false also appends a query string parameter, "_=[TIMESTAMP]", to the URL.

缓存默认值:true,false 用于 dataType 'script' 和 'jsonp'

如果设置为 false,它将强制请求的页面不被浏览器缓存。将缓存设置为 false 还会将查询字符串参数“_=[TIMESTAMP]”附加到 URL。

Examples

例子

  1. Globally disable caching for all future ajax requests. See .ajaxSetup()

    $.ajaxSetup({
      cache: false
    });
    
  2. Disable caching per request. See .ajax()

    $.ajax({
      url: "test.html",
      cache: false,
      success: function(html){
        $("#results").append(html);
      }
    });
    
  1. 全局禁用所有未来 ajax 请求的缓存。看.ajaxSetup()

    $.ajaxSetup({
      cache: false
    });
    
  2. 禁用每个请求的缓存。看.ajax()

    $.ajax({
      url: "test.html",
      cache: false,
      success: function(html){
        $("#results").append(html);
      }
    });
    

回答by Matt Bradley

If you are using jQuery's AJAX functions, try setting $.ajaxSetup( { cache: false } )at the top of your page, and let jQuery take care of it for you.

如果您正在使用 jQuery 的 AJAX 函数,请尝试$.ajaxSetup( { cache: false } )在页面顶部进行设置,让 jQuery 为您处理。

回答by Alex Turpin

Like most mentionned, you should use the jQuery cacheparameter, but for your information, it is often done not using Math.randombut with new Date().valueOf(), which is a time value pretty much guaranteed to be unique.

与大多数提到的一样,您应该使用 jQuerycache参数,但对于您的信息,它通常不使用Math.random但 with new Date().valueOf(),这是一个几乎可以保证唯一的时间值。

回答by Naftali aka Neal

window.location.hash = $(this).attr('href')
                            .substr(0,$(this).attr('href').length-5)
                            + Math.random();

回答by Ghazanfar Mir

Best option is to use timestamp with the request:

最好的选择是在请求中使用时间戳:

window.location.hash = $(this).attr('href')
                            .substr(0,$(this).attr('href').length-5)
                            + timestamp;