在网址后添加随机数 [英] Adding a random number after the URL

查看:128
本文介绍了在网址后添加随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用jQuery和Ajax更改网站内容而不重新加载页面的网站.该网站包含我经常更改的内容.但是页面以某种方式保存在缓存中,因此它不显示更改.

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.

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

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

我认为这与以下事实有关:页面始终具有相同的URL,所以我考虑在其中添加一个随机数,例如:

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:

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

(这不是我的代码,是通过googlin找到的),但这只会将我单击的链接ID添加到URL.我不知道在哪里放置"Math.random()"来添加随机数.

(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.

希望您能提供帮助!

推荐答案

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

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.

引用 .ajax()

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

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

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

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.

示例

  1. 全局禁用所有将来的ajax请求的缓存.请参见 .ajaxSetup()

$.ajaxSetup({
  cache: false
});

  • 禁用每个请求的缓存.参见 .ajax()

  • Disable caching per request. See .ajax()

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

  • 这篇关于在网址后添加随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆