如何使用JavaScript刷新页面?


当前回答

这适用于所有浏览器:

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>

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

    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

使用JavaScript刷新页面有多种无限方式:

location.reload()历史记录.go(0)location.href=位置.hreflocation.href=位置.pathnamelocation.replace(location.pathname)location.reload(false)如果我们需要从再次打开web服务器(例如文档内容动态更改),我们会将参数传递为true。

您可以继续列表中的创意:

window.location=窗口位置window.self.window.self.window.window.location=窗口位置…和其他534种方式

var方法=[“location.reload()”,“history.go(0)”,“location.href=位置.href”,“location.href=位置.路径名”,“location.replace(location.pathname)”,“location.reload(false)”];var$body=$(“body”);for(var i=0;i<methods.length;++i){(函数(cMethod){$body.append($(“<button>”{text:c方法}).on(“单击”,函数(){eval(cMethod);//不要怪我使用eval}));})(方法[i]);}按钮{背景:#2ecc71;边框:0;颜色:白色;字号:粗体;字体系列:“Monaco”,monospace;填充:10px;边界半径:4px;光标:指针;过渡:背景色0.5s轻松;边距:2px;}按钮:悬停{背景:#27ae60;}<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script>

这里所有的答案都很好。由于问题指定了如何使用jquery重新加载页面,我只是想为未来的读者添加更多内容。

jQuery是一个跨平台JavaScript库,旨在简化HTML的客户端脚本。~维基百科~

因此,您将了解jquery或jquery的基础是基于javascript的。因此,在简单的事情上,使用纯javascript要好得多。

但如果您需要jquery解决方案,这里有一个。

$(location).attr('href', '');

在按钮标记中使用onclick=“return location.reload();”。

<button id="refersh-page" name="refersh-page" type="button" onclick="return location.reload();">Refesh Page</button>