Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 强制浏览器重新加载页面(忽略缓存)_Javascript_Java_Spring_Browser Cache - Fatal编程技术网

Javascript 强制浏览器重新加载页面(忽略缓存)

Javascript 强制浏览器重新加载页面(忽略缓存),javascript,java,spring,browser-cache,Javascript,Java,Spring,Browser Cache,当我在web服务器上设置静态资源(如js、css、模板)时,我忘记了为它们设置缓存周期。默认情况下,web服务器将其设置为“永远缓存”(这是tomcat服务器和spring mvc) 但这没有帮助(我认为浏览器缓存了整个html页面,但它没有) 我尝试使用JS强制重新加载页面: 位置。重新加载(true) 但这并没有给我们带来多大帮助 如何强制浏览器重新加载缓存文件? 另外,我不发送任何缓存头来缓存我的html页面您可以在URL中附加一个版本号 http://mydomin.com/resour

当我在web服务器上设置静态资源(如js、css、模板)时,我忘记了为它们设置缓存周期。默认情况下,web服务器将其设置为“永远缓存”(这是tomcat服务器和spring mvc)

但这没有帮助(我认为浏览器缓存了整个html页面,但它没有)

  • 我尝试使用JS强制重新加载页面:

    位置。重新加载(true)

  • 但这并没有给我们带来多大帮助

    如何强制浏览器重新加载缓存文件?
    另外,我不发送任何缓存头来缓存我的html页面

    您可以在URL中附加一个版本号

    http://mydomin.com/resources/file.css?version=1
    
    比如,

    <html>
        <head>
            <link href="resources/file.css?version=1" type="text/css" rel="stylesheet"/>
        <head>
        <body></body>
    </html>
    

    我知道在这里没有jQuery或Ajax标记,但是重新加载.js文件的最简单方法是使用jQuery getScript()函数,这只是执行Ajax http请求的快捷方式

    $.getScript('../path_or_URL_to_your_js_file.js');
    
    这将加载附加了时间戳的外部资源,因此默认情况下,它将从服务器而不是缓存加载。在调用getScript函数之前,您只需要确保jquery已包含在内

    为了防止.css文件的缓存版本,可以在加载页面后使用jQuery/ajax在html头部动态构建链接元素,这将阻止加载缓存资源

    $('<link/>', {
       rel: 'stylesheet',
       type: 'text/css',
       href: 'path_to_the.css'
    }).appendTo('head');
    
    $(“”{
    rel:'样式表',
    键入:“text/css”,
    href:'path_to_the.css'
    }).appendTo(“head”);
    
    $.getScript('../path_or_URL_to_your_js_file.js');
    
    $('<link/>', {
       rel: 'stylesheet',
       type: 'text/css',
       href: 'path_to_the.css'
    }).appendTo('head');