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

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

How to reload page once using JQuery?

jquery

提问by Matoeil

How can I reload a page once on page load (an equivalent to pressing F5)

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

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

This loops itself. How can I make it happen only once?

这循环本身。我怎样才能让它只发生一次?

回答by Reeno

This will do it:

这将做到:

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

With your jQuery-Code:

使用您的 jQuery 代码:

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

回答by Reeno

Nowadays... HTML5 provides localStorage:

现在... 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
}


and this doesn't need jquery. Hope it helps, as it helped me.

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

回答by Thusitha Sumanadasa

<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>

Due to the if condition the page will reload only once.I faced this problem too and when I search ,I found a nice solution. This works for me fine.And thanks for http://www.webdeveloper.com/forum/showthread.php?267853-Auto-refresh-page-once-only-after-first-load

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

回答by pady

Add below line into your HTML file:

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

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

And call this function on onload of page,

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

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

回答by pady

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

repeats once

重复一次

回答by Murali Mopuru

Reload page with some query string for reference.

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

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';
   }

}

回答by Matoeil

thanks for your help meanwhile i have made it working this way

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

   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);

回答by TheOne LuKcian

There is an easier way (worked in my case)

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

If you are using jQuery, set an global variable in JS:

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

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

Then use this JS code in your jQuery page (inside <div data-role="page" id="myPage">)

然后在你的 jQuery 页面(里面<div data-role="page" id="myPage">)使用这个 JS 代码

<script>

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

            } 
         });
</script>

回答by Dominic Cerisano

I seem to have solved a similar problem with github static pages. I wanted to force reload of pages (ie no caching) since the caching was causing broken links.

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

The trouble with that was I could not use meta http-equiv OR htaccess to change caching since github overrides these settings.

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

None of the "reload only once" solutions would work across the back button.

没有一个“仅重新加载一次”解决方案适用于后退按钮。

The following seems to work on all browsers:

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

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

This forces a reload directly from the server whenever the user leavesa page (same effect as no caching). The dreaded loop is avoided.

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