如何使用JavaScript刷新页面?
当前回答
简单的Javascript解决方案:
location = location;
<buttonClick=“location=location;”>重新加载</button>
其他回答
<i id="refresh" class="fa fa-refresh" aria-hidden="true"></i>
<script>
$(document).on('click','#refresh',function(){
location.reload(true);
});
</script>
您不需要jQuery中的任何东西,要使用纯JavaScript重新加载页面,只需在location属性上使用reload函数,如下所示:
window.location.reload();
默认情况下,这将使用浏览器缓存(如果存在)重新加载页面。。。
如果您想强制重新加载页面,只需传递一个true值来重新加载方法,如下所示。。。
window.location.reload(true);
此外,如果您已经在窗口范围内,您可以删除窗口并执行以下操作:
location.reload();
jQueryLoad函数还可以执行页面刷新:
$('body').load('views/file.html', function () {
$(this).fadeIn(5000);
});
有很多方法可以重新加载当前页面,但使用这些方法,您可以看到页面更新,但不会有很少的缓存值,因此要克服这个问题,或者如果您希望发出硬请求,请使用以下代码。
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
下面是一些代码行,您可以使用jQuery重新加载页面。
它使用jQuery包装器并提取本地dom元素。
如果您只想在代码上获得jQuery的感觉,而不关心代码的速度/性能,请使用此选项。
只需从1到10中选择适合您的需求,或者根据模式和之前的答案添加更多内容。
<script>
$(location)[0].reload(); //1
$(location).get(0).reload(); //2
$(window)[0].location.reload(); //3
$(window).get(0).location.reload(); //4
$(window)[0].$(location)[0].reload(); //5
$(window).get(0).$(location)[0].reload(); //6
$(window)[0].$(location).get(0).reload(); //7
$(window).get(0).$(location).get(0).reload(); //8
$(location)[0].href = ''; //9
$(location).get(0).href = ''; //10
//... and many other more just follow the pattern.
</script>
推荐文章
- 如何为Firebase构建云函数,以便从多个文件部署多个函数?
- 如何发送推送通知到web浏览器?
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- 用jQuery检查Internet连接是否存在?
- 如何使用滑动(或显示)函数在一个表行?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法