如何使用JavaScript刷新页面?


当前回答

您可能想使用

location.reload(forceGet)

forceGet是一个布尔值,也是可选的。

默认值为false,从缓存中重新加载页面。

如果您希望强制浏览器从服务器获取页面,以清除缓存,请将此参数设置为true。

或者只是

location.reload()

如果您想快速方便地使用缓存。

其他回答

如果您正在使用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);

要使用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。。。,刷新页面应该意味着向页面获取新的和更新的内容。无论如何,这个答案很快就会消失/

您应该使用以下代码

window.location.reload();

它是JavaScript中最短的。

window.location = '';

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

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