如何使用JavaScript刷新页面?
当前回答
window.location.reload()将从服务器重新加载,并将再次加载所有数据、脚本、图像等。
因此,如果您只想刷新HTML,window.location=document.URL将返回得更快,流量更少。但如果URL中有散列(#),则不会重新加载页面。
其他回答
有很多方法可以重新加载当前页面,但使用这些方法,您可以看到页面更新,但不会有很少的缓存值,因此要克服这个问题,或者如果您希望发出硬请求,请使用以下代码。
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
<i id="refresh" class="fa fa-refresh" aria-hidden="true"></i>
<script>
$(document).on('click','#refresh',function(){
location.reload(true);
});
</script>
你可以用两种方式写。第一种是重新加载页面的标准方式,也称为简单刷新
location.reload(); //simple refresh
另一种叫做硬刷新。在这里传递布尔表达式并将其设置为true。这将重新加载页面,销毁旧缓存并从头开始显示内容。
location.reload(true);//hard refresh
您可以使用JavaScript location.reload()方法。此方法接受布尔参数。正确或错误。如果参数为真;页面总是从服务器重新加载。如果是假的;这是默认设置,或者使用空参数浏览器从其缓存中重新加载页面。
带真参数
<button type="button" onclick="location.reload(true);">Reload page</button>
带默认/假参数
<button type="button" onclick="location.reload();">Reload page</button>
使用jquery
<button id="Reloadpage">Reload page</button>
<script type="text/javascript">
$('#Reloadpage').click(function() {
location.reload();
});
</script>
这适用于所有浏览器:
location.reload();