如何使用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>

其他回答

您应该使用以下代码

window.location.reload();

您不需要jQuery中的任何东西,要使用纯JavaScript重新加载页面,只需在location属性上使用reload函数,如下所示:

window.location.reload();

默认情况下,这将使用浏览器缓存(如果存在)重新加载页面。。。

如果您想强制重新加载页面,只需传递一个true值来重新加载方法,如下所示。。。

window.location.reload(true);

此外,如果您已经在窗口范围内,您可以删除窗口并执行以下操作:

location.reload();

如果当前页面是通过POST请求加载的,您可能需要使用

window.location = window.location.pathname;

而不是

window.location.reload();

因为如果在POST请求加载的页面上调用window.location.reload()将提示确认。

jQueryLoad函数还可以执行页面刷新:

$('body').load('views/file.html', function () {
    $(this).fadeIn(5000);
});

use

location.reload();

or

window.location.reload();