如何使用JQuery重新加载页面?

时间:2013-11-14 15:13:47

标签: jquery

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

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

这循环自己。 我怎么能只让它发生一次?

9 个答案:

答案 0 :(得分:10)

这样做:

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

使用jQuery-Code:

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

答案 1 :(得分:10)

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

/* if my var reload isn't set locally.. in the first time it will be true */
if (!localStorage.getItem("reload")) {
    /* set reload locally and then reload the page */
    localStorage.setItem("reload", "true");
    location.reload();
}
/* after reload clear the localStorage */
else {
    localStorage.removeItem("reload");
    // localStorage.clear(); // an option
}

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

答案 2 :(得分: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

答案 3 :(得分:2)

在HTML文件中添加以下行:

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

在onload of page上调用此函数,

<script type="text/javascript">
onload = function() {
    if ($("#refreshed").val() == "no") {
        location.reload();
        $("#refreshed").val("yes"); 
    }
}
</script>

答案 4 :(得分: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';
   }

}

答案 5 :(得分:1)

echo ("<script type='text/javascript'>location.replace('http://url.goes.here');</script>");

重复一次

答案 6 :(得分: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);

答案 7 :(得分: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>

答案 8 :(得分:0)

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

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

所有&#34;仅重新加载一次&#34;解决方案可以在后退按钮上工作。

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

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

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

相关问题