首页 > 解决方案 > 什么命令相当于 Shift + F5 用于 JS 或 jQuery?

问题描述

我想清除缓存并硬重新加载浏览器,但是当文档准备好时如何使用 JS/jQuery 命令来做到这一点?

我的参考:

  1. 第一个链接
  2. 第二个链接
  3. 第三链接

我在下面尝试过,但除了评论外没有工作,this one但它不断重新加载,永不停止。为什么会这样?如何让它只重新加载 1 次。

$(document).ready(function(){
    // window.location.reload(false);
    // document.location.reload(true);
    // window.location.reload(true);
    // window.location.href = window.location.href;
    // location.reload(true); //this one
});

更新:谷歌搜索并尝试了这个,但没有工作。

sessionStorage.setItem("origin", window.location.href);

$(document).ready(function(){
    if(window.location.href == sessionStorage.getItem("origin")){
        window.location.reload();
        sessionStorage.removeItem('origin');
    }
});

标签: javascriptjquery

解决方案


您可以localStorage为此设置一个,只需检查您localStorage是否有值。如果没有,请先重新加载,然后设置一个值。看看下面的例子:

$(document).ready(function () {

    var reload = localStorage.getItem('reload');

    console.log(reload);

    if (reload === null) {
        location.reload(true);
        localStorage.setItem('reload', "stop");
    }

});

推荐阅读