Javascript 仅刷新/重新加载网页一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27395483/
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-08-23 00:06:07  来源:igfitidea点击:

Refresh/Reload web page only once

javascriptphpjqueryhtmlpage-refresh

提问by Rohit Jindal

I've searched high and low and am unable to find any scripts or instructions on how to have a page automatically refresh ONLY ONCEafter initially being loaded/viewed.

我已经搜索了高低,但无法找到有关如何ONLY ONCE在最初加载/查看页面后自动刷新页面的任何脚本或说明。

I implemented a javascript code which states this: if the page is loaded completely, refresh the page immediately, but only once.

我实现了一个 javascript 代码,它说明了这一点:如果页面完全加载,立即刷新页面,但只能刷新一次。

window.onload = function () {window.location.reload()}

But this gives a loop without the "only once".

但这给出了一个没有“只有一次”的循环。

I also Implemented meta refreshbut it also gives a loop.

我也实现了,meta refresh但它也提供了一个循环。

<meta http-equiv="refresh" content= "0;URL=http://www.example.com" />

Please tell me how can i use body onloadrefresh functionality only oncethat is for first time and not every time the php page is refreshed .

请告诉我如何使用第一次而不是每次刷新 php 页面时的正文onload刷新功能only once

I appreicate your immediate response. Thanks

我很欣赏你的即时反应。谢谢

回答by dashtinejad

You should keep a flag in cookie, server session, or local storage. For example:

您应该在 cookie、服务器会话或本地存储中保留一个标志。例如:

window.onload = function () {
    if (! localStorage.justOnce) {
        localStorage.setItem("justOnce", "true");
        window.location.reload();
    }
}

回答by low_rents

i don't know why you wanna do that. but you can just use php, javascript/jquery together to approach this:

我不知道你为什么要那样做。但是你可以一起使用 php、javascript/jquery 来解决这个问题:

<?php
    if ($_GET["reloaded"] != 1) { 

        echo '
            <script>
                $(function () {
                    window.location.href = "my_page.php?reloaded=1";
                });
            </script>
       '; 

    }
?>

explanation: when you load the page for the first time $_GET["reloaded"]is not set, so the javascript is echoed. i used jquery's document ready function here, to be sure that your DOM is fully loaded before it reloads the page. when reloading you set the GET variable reloadedto 1.
thus php will not echo the javascript code again on reload.

解释:第一次加载页面时$_GET["reloaded"]没有设置,所以回显了javascript。我在这里使用了 jquery 的文档就绪功能,以确保您的 DOM 在重新加载页面之前已完全加载。重新加载时,您将 GET 变量设置reloaded1.
因此 php 不会在重新加载时再次回显 javascript 代码。

回答by Darren

What you want to do is set a cookie, to check if the user has refreshed or not. The following is basically pseudo-code for how you should do it.

您想要做的是设置一个 cookie,以检查用户是否已刷新。以下基本上是关于你应该如何做的伪代码。

Javascript

Javascript

if(!COOKIE_EXISTS){
    SET_COOKIE();
    REDIRECT_TO_DESIRED_PAGE;
}

Php

php

if(!isset($_COOKIE['page_was_refreshed'])){
    setcookie("CookieName", "CookieValue", time() + (10 * 365 * 24 * 60 * 60));
    die(header("Location: your/url/to/redirect"));
}