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

发布于 2024-11-29 09:45:10 字数 83 浏览 2 评论 0原文

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

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事↓在人 2024-12-06 09:45:10

那么您只想浏览器刷新一次?做这样的事情。

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

或 jquery

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

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

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

$(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.

等风来 2024-12-06 09:45:10

您总是必须使用一些 JavaScript。您想要的是刷新代码在页面完全加载时运行。您可以使用 HTML onload 事件,但这样做有问题(例如,它会在加载任何图像之前触发)。如果您想的话,我建议使用 JQuery 的 ready() 事件确保它在整个页面加载后触发。

示例:

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

使其仅在首页加载时触发有点棘手。您可以向 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);
    }
});

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

$(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;
    }
});

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

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 onload event, 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.

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:

$(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文