如何使用JavaScript刷新页面?


当前回答

它是JavaScript中最短的。

window.location = '';

其他回答

可能最短(12个字符)-使用历史

history.go()

下面是一个使用jQuery异步重新加载页面的解决方案。它避免了window.location=window.location引起的闪烁。这个示例显示了一个连续重新加载的页面,就像在仪表板中一样。它经过战斗测试,正在时代广场的信息显示电视上运行。

<!DOCTYPE html>
<html lang="en">
  <head>
    ...
    <meta http-equiv="refresh" content="300">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
    <script>
    function refresh() {
      $.ajax({
        url: "",
        dataType: "text",
        success: function(html) {
          $('#fu').replaceWith($.parseHTML(html));
          setTimeout(refresh,2000);
        }
      });
    }
    refresh();
    </script>
  </head>
  <body>
    <div id="fu">
      ...
    </div>
  </body>
</html>

笔记:

直接像$.get(“”,function(data){$(document.body).html(data)})那样使用$.ajax会导致css/js文件被缓存破坏,即使您使用cache:true,这就是我们使用parseHTML的原因parseHTML不会找到一个body标签,所以你的整个身体都需要放在一个额外的div中,我希望这些知识有一天能帮助你,你可以猜到我们是如何为那个div选择id的使用http equiv=“refresh”,以防javascript/服务器打嗝出现问题,然后页面仍然会重新加载,而不会接到电话这种方法可能会以某种方式泄漏内存,http equiv刷新修复了

window.location.reload()将从服务器重新加载,并将再次加载所有数据、脚本、图像等。

因此,如果您只想刷新HTML,window.location=document.URL将返回得更快,流量更少。但如果URL中有散列(#),则不会重新加载页面。

如果您正在使用jQuery并希望刷新,请尝试在javascript函数中添加jQuery:

我想在单击oh an h3时从页面中隐藏iframe,对我来说这是有效的,但我无法单击允许我查看iframe的项目,除非我手动刷新浏览器。。。不理想。

我尝试了以下方法:

var hide = () => {
    $("#frame").hide();//jQuery
    location.reload(true);//javascript
};

将简单的Jane javascript与jQuery混合使用应该会奏效。

// code where hide (where location.reload was used)function was integrated, below    
    iFrameInsert = () => {
        var file = `Fe1FVoW0Nt4`;
        $("#frame").html(`<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/${file}\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe><h3>Close Player</h3>`);
        $("h3").enter code hereclick(hide);
}

// View Player 
$("#id-to-be-clicked").click(iFrameInsert);
<i id="refresh" class="fa fa-refresh" aria-hidden="true"></i>

<script>
$(document).on('click','#refresh',function(){
   location.reload(true);
});
</script>