如何使用JavaScript刷新页面?


当前回答

有很多方法可以重新加载当前页面,但使用这些方法,您可以看到页面更新,但不会有很少的缓存值,因此要克服这个问题,或者如果您希望发出硬请求,请使用以下代码。

    location.reload(true);
    //Here, it will make a hard request or reload the current page and clear the cache as well.


    location.reload(false); OR location.reload();
    //It can be reload the page with cache

其他回答

我找到了

window.location.href = "";

or

window.location.href = null;

还进行页面刷新。

这使得移除任何哈希值后重新加载页面变得非常容易。当我在iOS模拟器中使用AngularJS时,这非常好,因此我不必重新运行应用程序。

简单的Javascript解决方案:

 location = location; 

<buttonClick=“location=location;”>重新加载</button>

$(document).on("click", "#refresh_btn", function(event) 
{
     window.location.replace(window.location.href);
});

您可能需要使用

 location.reload()

或可能需要使用

location.reload(forceGet)

forceGet是一个布尔值,也是可选的。

如果您希望强制浏览器从服务器获取页面,以便同时清除缓存,请将此参数设置为true

window.location.reload()将从服务器重新加载,并将再次加载所有数据、脚本、图像等。

因此,如果您只想刷新HTML,window.location=document.URL将返回得更快,流量更少。但如果URL中有散列(#),则不会重新加载页面。