如何使用JavaScript刷新页面?


当前回答

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

window.location = window.location.pathname;

而不是

window.location.reload();

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

其他回答

使用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并希望刷新,请尝试在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);
$(document).on("click", "#refresh_btn", function(event) 
{
     window.location.replace(window.location.href);
});

use

location.reload();

or

window.location.reload();

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

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