简体   繁体   English

如何强制页面仅在每次访问时刷新一次

[英]How to force a page to refresh only once every visit

I've tried about 10 different ways that I found on here and the only found one that works close. 我尝试了大约10种在这里找到的不同方法,并且找到了唯一可以关闭的方法。 I'm having an issue with Hostgator all of a sudden not displaying new content on php pages that access and display data from text files and/or databases. 我在Hostgator遇到问题,突然不在访问和显示文本文件和/或数据库数据的php页面上不显示新内容。 These pages have been in place for years working fine and continue to do so on other hosts. 这些页面已经存在多年,并且可以在其他主机上正常运行。 The only problem I have with the script below is that it won't refresh the page on the first visit. 我下面的脚本唯一的问题是它不会在第一次访问时刷新页面。 After the first refresh it will then show changes. 第一次刷新后,它将显示更改。 I need something that will refresh all the time regardless of a visitors history or browser settings. 无论访问者的历史记录或浏览器设置如何,我都需要始终刷新的内容。

<script type='text/javascript'>
     (function()
     {
        if( window.localStorage ) {
           if( !localStorage.getItem( 'firstLoad' ) ) {
               localStorage[ 'firstLoad' ] = true;
               window.location.reload();
            }
          else
              localStorage.removeItem( 'firstLoad' );
        }
    })();

</script> 

you can use a cookie: 您可以使用Cookie:

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

(function()
{
    var value = readCookie('mycookiereload');
    if(value == null)
    {
        var date = new Date();
        date.setTime(date.getTime()+(3600000));
        var expires = "; expires="+date.toGMTString();
        document.cookie = "mycookiereload=1"+expires+"; path=/";
        alert("going to reload");
        window.location.reload();
    }
    else
    {
        var date = new Date();
        date.setTime(date.getTime()-(3600000));
        var expires = "; expires="+date.toGMTString();
        document.cookie = "mycookiereload="+expires+"; path=/";
        alert("kill cookie");
    }
})();

this will reload the page once. 这将重新加载页面一次。 everytime you visit a page, it checks the cookie, if it's not there, creates and reloads, if it's there, removes the cookie to get the same effect again. 每次您访问页面时,它都会检查cookie(如果不存在),创建并重新加载(如果存在),然后删除cookie,以再次获得相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM