首页加载后一次刷新页面

时间:2011-08-08 16:44:46

标签: javascript jquery refresh reload

我想实现一个JavaScript代码,声明: 如果页面已完全加载,请立即刷新页面,但只刷新一次。 我被困在“只有一次”:

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

这给出了一个没有“只有一次”的循环。如果这有帮助,则加载jQuery。

17 个答案:

答案 0 :(得分:83)

我会说使用hash,就像这样:

window.onload = function() {
    if(!window.location.hash) {
        window.location = window.location + '#loaded';
        window.location.reload();
    }
}

答案 1 :(得分:30)

当我遇到这个问题时,我会搜索到这里,但大多数答案都试图修改现有网址。这是另一个适用于localStorage的答案。

<script type='text/javascript'>

(function()
{
  if( window.localStorage )
  {
    if( !localStorage.getItem('firstLoad') )
    {
      localStorage['firstLoad'] = true;
      window.location.reload();
    }  
    else
      localStorage.removeItem('firstLoad');
  }
})();

</script>

答案 2 :(得分:27)

<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条件,页面只会重新加载一次。我也遇到了这个问题,当我搜索时,我发现了这个很好的解决方案。 这对我很有用。

答案 3 :(得分:9)

选中此链接它包含一个java脚本代码,您只能用一次刷新页面

http://www.hotscripts.com/forums/javascript/4460-how-do-i-have-page-automatically-refesh-only-once.html

刷新页面的方法有多种:

<强>解决方法1

每次打开页面时刷新页面使用:

<head>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
</head>

<强> sollution2:

<script language=" JavaScript" >
<!-- 
function LoadOnce() 
{ 
window.location.reload(); 
} 
//-->
</script>

然后改变你的说法

<Body onLoad=" LoadOnce()" >

<强> solution3:

response.setIntHeader("Refresh", 1);

但是此解决方案将根据您指定的时间多次刷新页面

我希望能帮到你

答案 4 :(得分:4)

<script>

function reloadIt() {
    if (window.location.href.substr(-2) !== "?r") {
        window.location = window.location.href + "?r";
    }
}

setTimeout('reloadIt()', 1000)();

</script>

这很好用

答案 5 :(得分:3)

我把它放在我希望单次重新加载的页面的标题内:

<?php if(!isset($_GET['mc'])) {
echo '<meta http-equiv="refresh" content= "0;URL=?mc=mobile" />';
} ?>

值“mc”可以设置为您想要的任何值,但两者必须匹配在2行中。并且“= mobile”可以是“= whatyouwant”它只需要一个值来停止刷新。

答案 6 :(得分:1)

</body>代码后:

<script type="text/javascript">
if (location.href.indexOf('reload')==-1)
{
   location.href=location.href+'?reload';
}
</script>

答案 7 :(得分:0)

        var foo = true;
        if (foo){
            window.location.reload(true);
            foo = false;

        }

答案 8 :(得分:0)

您可以创建一个可验证的 once = false,然后使用 if else 重新加载您的页面,就像 once == false 重新加载页面并重新加载一次一样。

答案 9 :(得分:0)

这是使用setTimeout的另一种解决方案,虽然不完美,但是可以使用:

它在当前网址中需要一个参数,因此只需对当前网址进行如下显示即可:

www.google.com?time=1

以下代码使页面仅重新加载一次:

 // Reload Page Function //
    // get the time parameter //
        let parameter = new URLSearchParams(window.location.search);
          let time = parameter.get("time");
          console.log(time)//1
          let timeId;
          if (time == 1) {
    // reload the page after 0 ms //
           timeId = setTimeout(() => {
            window.location.reload();//
           }, 0);
   // change the time parameter to 0 //
           let currentUrl = new URL(window.location.href);
           let param = new URLSearchParams(currentUrl.search);
           param.set("time", 0);
   // replace the time parameter in url to 0; now it is 0 not 1 //
           window.history.replaceState({}, "", `${currentUrl.pathname}?${param}`);
        // cancel the setTimeout function after 0 ms //
           let currentTime = Date.now();
           if (Date.now() - currentTime > 0) {
            clearTimeout(timeId);
           }
          }

可接受的答案使用最少的代码,并且易于理解。我只是为此提供了另一种解决方案。

希望这对其他人有帮助。

答案 10 :(得分:0)

最后,经过两个月的研究,我得到了一个重新加载页面的解决方案。

它在我的客户端JS项目中运行良好。

我写了一个函数,只在重载页面下面一次。

1)首先获得浏览器缩放时间

2)获取当前时间戳

3)浏览器domloading time + 10秒

4)如果浏览器缩放时间比当前时间戳大10秒,那么页面可以通过&#34; reloadPage();&#34;

刷新

但如果它不超过10秒,则表示页面刚重新加载,因此不会重复重新加载。

5)因此,如果你打电话给&#34; reloadPage();&#34;在js文件页面的某个地方的函数只会重新加载一次。

希望能帮助某人

// Reload Page Function //
                function reloadPage() {
                    var currentDocumentTimestamp = new Date(performance.timing.domLoading).getTime();
                    // Current Time //
                    var now = Date.now();
                    // Total Process Lenght as Minutes //
                    var tenSec = 10 * 1000;
                    // End Time of Process //
                    var plusTenSec = currentDocumentTimestamp + tenSec;
                    if (now > plusTenSec) {
                        location.reload();
                    }
                }


                // You can call it in somewhere //
                reloadPage();

答案 11 :(得分:0)

使用window.localStorage ...像这样

var refresh = $window.localStorage.getItem('refresh');
console.log(refresh);
if (refresh===null){
    window.location.reload();
    $window.localStorage.setItem('refresh', "1");
}

它为我工作

答案 12 :(得分:0)

使用此

<body onload =  "if (location.search.length < 1){window.location.reload()}">

答案 13 :(得分:0)

请尝试使用以下代码

var windowWidth = $(window).width();

$(window).resize(function() {
    if(windowWidth != $(window).width()){
    location.reload();

    return;
    }
});

答案 14 :(得分:0)

试试这个

var element = document.getElementById('position');        
element.scrollIntoView(true);`

答案 15 :(得分:0)

您需要使用GET或POST信息。 GET最简单。您的JS会检查URL,如果找不到某个参数,它不会只刷新页面,而是将用户发送到“不同”的URL,这将是相同的URL,但其中包含GET参数

例如:
http://example.com - &gt;将刷新
http://example.com?refresh=no - &gt;不会刷新

如果你不想要凌乱的URL,那么我会在正文的开头包含一些PHP,它会回显一个隐藏的值,这个值表示是否在初始化中包含了不刷新页面的必要POST参数页面请求。在那之后,你需要包含一些JS来检查该值,并在必要时用该POST信息刷新页面。

答案 16 :(得分:-1)

使用rel =&#34; external&#34; 如下所示是

<li><a href="index.html" rel="external" data-theme="c">Home</a></li>
相关问题