Javascript 如何在加载 HTML 页面时清除缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43516436/
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-08-23 01:58:59  来源:igfitidea点击:

How to clear cache memory on load of HTML page?

javascriptjqueryajax

提问by Amit Gujarathi

I'm developing a website. But it caches user name and password in cache block which can be accessed using hacking software like winhex. I want to clear cache

我正在开发一个网站。但是它将用户名和密码缓存在缓存块中,可以使用 winhex 等黑客软件访问。我想清除缓存

$(".object-position").livequery("change", function() {
    $("#objects-list input").attr('disabled', true);
    var action = $(this).attr('name');
    var position = $(this).attr('value');
    var id = $(this).attr("id");
    var model = id.split("-")[0];
    var object_id = id.split("-")[1];

    $("#loader").show();
    $("#loader").fadeIn(200);

    $.ajax({
        type: "POST",
        async: true,
        url: "/manage/update_position/",
        data: "action=" + action + "&model=" + model + "&object_id=" + object_id + "&position=" + position,
        dataType: "json",
        success: function(data){
            $("#loader").fadeOut("fast", function () {
                $("#loader").hide();
            });
            $("objects-list").html(data["html"]);
            $("#message").show();
            $("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
            setTimeout(function(){
                $("#message").fadeOut("slow", function () {
                    $("#message").hide();
                });
            }, 1500); 
        }
    });
    $("#objects-list input").attr("disabled", false);
    return false;
});

回答by SeF

This meta code should work with most browsers for web content. However, for resource files (javascript, images, css) your mileage may vary. Most cache busting strategies involve changing the name of your resource files (perhaps dynamically) or using Apache rewrite rules to pretend that the names are changed. This google search should put you on the right track.(cache busting strategy for js)

此元代码应该适用于大多数浏览器的 Web 内容。但是,对于资源文件(javascript、图像、css),您的使用范围可能会有所不同。大多数缓存破坏策略涉及更改资源文件的名称(可能是动态的)或使用 Apache 重写规则来假装名称已更改。这个谷歌搜索应该让你走上正轨。(js的缓存破坏策略)

<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />

回答by Amit Gujarathi

Adding this meta tag will help you solve your problem .

添加此元标记将帮助您解决问题。

<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />