如何使用JavaScript刷新页面?
当前回答
下面是一些代码行,您可以使用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>
其他回答
您可能想使用
location.reload(forceGet)
forceGet是一个布尔值,也是可选的。
默认值为false,从缓存中重新加载页面。
如果您希望强制浏览器从服务器获取页面,以清除缓存,请将此参数设置为true。
或者只是
location.reload()
如果您想快速方便地使用缓存。
下面是一些代码行,您可以使用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>
这里所有的答案都很好。由于问题指定了如何使用jquery重新加载页面,我只是想为未来的读者添加更多内容。
jQuery是一个跨平台JavaScript库,旨在简化HTML的客户端脚本。~维基百科~
因此,您将了解jquery或jquery的基础是基于javascript的。因此,在简单的事情上,使用纯javascript要好得多。
但如果您需要jquery解决方案,这里有一个。
$(location).attr('href', '');
要使用jQuery重新加载页面,请执行以下操作:
$.ajax({
url: "",
context: document.body,
success: function(s,x){
$(this).html(s);
}
});
这里我使用的方法是Ajax jQuery。我在Chrome 13上测试了它。然后我将代码放入将触发重载的处理程序中。URL为“”,表示此页面。
编辑:不幸的是,我必须很快记下这个答案,因为问题已经改变,用户似乎不太清楚他们在问什么。一些用户评论说,刷新与重新加载页面不同;查看此问题的修订[https://stackoverflow.com/posts/5404839/revisions]据我所知,最初的问题是用AJAX刷新页面,这是ASYNCHRONOUS。。。,刷新页面应该意味着向页面获取新的和更新的内容。无论如何,这个答案很快就会消失/
你们可能需要使用
location.reload(forceGet)
forceGet是一个布尔值,也是可选的。
默认值为false,这将重新加载缓存的页面。