8

如何在页面加载时重新加载页面(相当于按F5

$( window ).load(function() {   
    window.location.reload(true); 
});

这会自行循环。我怎样才能让它只发生一次?

4

9 回答 9

15

如今...... HTML5提供了localStorage:

/* if my "reload" var isn't set locally.. getItem will be false */
if (!localStorage.getItem("reload")) {
    /* set reload to true and then reload the page */
    localStorage.setItem("reload", "true");
    location.reload();
}
/* after reloading remove "reload" from localStorage */
else {
    localStorage.removeItem("reload");
    // localStorage.clear(); // or clear it, instead
}

这不需要jquery。希望它有所帮助,因为它帮助了我。

于 2015-02-12T14:10:51.807 回答
14

这将做到:

if (window.location.href.indexOf('reload')==-1) {
     window.location.replace(window.location.href+'?reload');
}

使用您的 jQuery 代码:

$( window ).load(function() {
    if (window.location.href.indexOf('reload')==-1) {
         window.location.replace(window.location.href+'?reload');
    }
});
于 2013-11-14T15:29:13.113 回答
4
<script type="text/javascript">
$(document).ready(function(){

    //Check if the current URL contains '#'
    if(document.URL.indexOf("#")==-1)
    {
    // Set the URL to whatever it was plus "#".
    url = document.URL+"#";
    location = "#";

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

    }
    });
</script>

由于 if 条件,页面只会重新加载一次。我也遇到了这个问题,当我搜索时,我找到了一个很好的解决方案。这对我来说很好。感谢http://www.webdeveloper.com/forum/showthread.php?267853-Auto-refresh-page-once-only-after-first-load

于 2014-04-25T11:10:32.217 回答
2

将以下行添加到您的 HTML 文件中:

    `<input name="refreshed" value="no" id="refreshed" />`<input name="refreshed" value="no" id="refreshed" />

并在页面加载时调用此函数,

<script type="text/javascript">
onload = function() {
    if ($("#refreshed").val() == "no") {
        location.reload();
        $("#refreshed").val("yes"); 
    }
}
</script>
于 2015-04-01T07:13:16.437 回答
1

使用一些查询字符串重新加载页面以供参考。

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}


$(window).load(function() {   

   if(getParameterByName('reloaded')){
      //loaded once
   }else{
      window.location = window.location.href+'?reloaded=true';
   }

}
于 2013-11-14T15:31:29.770 回答
1
echo ("<script type='text/javascript'>location.replace('http://url.goes.here');</script>");

重复一次

于 2014-07-11T06:18:41.860 回答
0

有一种更简单的方法(在我的情况下有效)

如果您使用的是 jQuery,请在 JS 中设置一个全局变量:

<script type="text/javascript">
    var only_once = "0";
</script>

然后在您的 jQuery 页面(内部<div data-role="page" id="myPage">)中使用此 JS 代码

<script>

        $('#myPage').bind('pageshow', function() { 
            if (only_once==1) {
                only_once = 0 ;
                location.reload();

            } 
         });
</script>
于 2014-07-21T10:56:15.963 回答
0

感谢您的帮助,同时我已经使它以这种方式工作

   function SetCookie (name, value) {
    var argv=SetCookie.arguments;
    var argc=SetCookie.arguments.length;
    var expires=(argc > 2) ? argv[2] : null;
    var path=(argc > 3) ? argv[3] : null;
    var domain=(argc > 4) ? argv[4] : null;
    var secure=(argc > 5) ? argv[5] : false;
    document.cookie=name+"="+escape(value)+
        ((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
        ((path==null) ? "" : ("; path="+path))+
        ((domain==null) ? "" : ("; domain="+domain))+
        ((secure==true) ? "; secure" : "");
}



    function getCookie(c_name)
    {
        var c_value = document.cookie;
        var c_start = c_value.indexOf(" " + c_name + "=");
        if (c_start == -1)
        {
            c_start = c_value.indexOf(c_name + "=");
        }
        if (c_start == -1)
        {
            c_value = null;
        }
        else
        {
            c_start = c_value.indexOf("=", c_start) + 1;
            var c_end = c_value.indexOf(";", c_start);
            if (c_end == -1)
            {
                c_end = c_value.length;
            }
            c_value = unescape(c_value.substring(c_start,c_end));
        }
        return c_value;
    }   




if (getCookie('first_load'))        
    {
        if (getCookie('first_load')==true)
        {
            window.location.reload(true); // force refresh page-liste
            SetCookie("first_load",false);
        }
    }
    SetCookie("first_load",true);
于 2013-11-14T15:39:07.163 回答
0

我似乎已经解决了与 github 静态页面类似的问题。我想强制重新加载页面(即没有缓存),因为缓存导致链接断开。

问题是我无法使用 meta http-equiv 或 htaccess 来更改缓存,因为 github 覆盖了这些设置。

“仅重新加载一次”解决方案都不能通过后退按钮起作用。

以下似乎适用于所有浏览器:

<body onbeforeunload="return window.location.reload(true)">

这会在用户离开页面时直接从服务器重新加载(与没有缓存的效果相同)。避免了可怕的循环。

于 2015-08-08T23:36:44.483 回答