如何使用JavaScript刷新页面?


当前回答

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

其他回答

你可以用两种方式写。第一种是重新加载页面的标准方式,也称为简单刷新

location.reload(); //simple refresh

另一种叫做硬刷新。在这里传递布尔表达式并将其设置为true。这将重新加载页面,销毁旧缓存并从头开始显示内容。

location.reload(true);//hard refresh

这适用于所有浏览器:

location.reload();

您可能想使用

location.reload(forceGet)

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

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

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

或者只是

location.reload()

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

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

window.location = window.location.pathname;

而不是

window.location.reload();

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

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

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