javascript 页面加载后如何刷新浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7013394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:41:30  来源:igfitidea点击:

how to refresh the browser after the page is loaded

javascriptpage-refresh

提问by srini

I have a scenario to refresh the browser after the page is loaded for first time or one time. Because the data is not showing properly when the pages is loaded,and when i refresh the browser it is showing.I don't know the reason.

我有一个场景在第一次或一次加载页面后刷新浏览器。因为当页面加载时数据没有正确显示,当我刷新浏览器时它正在显示。我不知道原因。

Many Thanks

非常感谢

回答by Akinos

So you only want the browser to refresh once? Do something like this.

所以你只希望浏览器刷新一次?做这样的事情。

window.onload = function(){
    if(!document.location.hash){
        window.location = "#loaded";
    }
}

or jquery

或 jquery

$(document).ready(function(){
    if(document.location.hash){
        window.location = "#loaded";
    }
});

But in all honesty this is just a temporary solution. As much as you try to cover it up with quick fixes. I guarantee it will come back to haunt you. Well written and structured code will last a lifetime and can always be reused on future projects.

但老实说,这只是一个临时解决方案。就像你试图用快速修复来掩盖它一样。我保证它会回来困扰你。编写好的和结构化的代码将持续一生,并且始终可以在未来的项目中重复使用。

回答by andrewtc

Invariably, you're going to have to use some JavaScript. What you want is for your refresh code to run when the page is completely loaded. You could use the HTML onloadevent, but there are problems with this (e.g. it will fire before any images are loaded). I would suggest using JQuery's ready()event, if you want to be sure it fires after the entire page is loaded.

不可避免地,您将不得不使用一些 JavaScript。您想要的是在页面完全加载时运行刷新代码。您可以使用 HTMLonload事件,但是这有问题(例如,它会在加载任何图像之前触发)。我建议使用 JQuery 的ready()event,如果你想确保它在整个页面加载后触发。

Example:

例子:

// NOTE: This will only work with JQuery loaded.
$(document).ready(function(){
    location.reload(true);
});

Making this only fire on the first page load is a bit more tricky. You could add an anchor suffix to the URL to keep track of whether you've refreshed the page yet or not, and then only refresh if it is not present in the URL:

让它只在第一页加载时触发有点棘手。您可以向 URL 添加锚点后缀以跟踪您是否已刷新页面,然后仅在 URL 中不存在时才刷新:

$(document).ready(function(){
    if(location.hash != "#")
    {
        // Set the URL to whatever it was plus "#".
        // (The page will NOT automatically reload.)
        location = "#";

        // Reload the page.
        location.reload(true);
    }
});

Alternatively, you could use the query string, since this will automatically refresh the page when changed:

或者,您可以使用查询字符串,因为这会在更改时自动刷新页面:

$(document).ready(function(){
    var marker = 'r'; // 'r' is for "refreshed"
    if(location.search != "?"+marker)
    {
        // Set the URL to whatever it was plus "?r".
        // (This will automatically force a page reload.)
        location = "?"+marker;
    }
});

Caveat:With either of these samples, if your user bookmarks the page after the "#" or "?r" tag has been added to the URL, the page won't refresh when they revisit the page. If you want it to be bulletproof, you might have to use a cookie instead.

警告:对于这些示例中的任何一个,如果您的用户在将“#”或“?r”标记添加到 URL 后将页面添加为书签,则当他们重新访问该页面时,该页面将不会刷新。如果您希望它是防弹的,则可能必须使用 cookie。