Javascript 第一个页面加载后一次页面刷新

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

One time page refresh after first page load

javascriptjqueryrefreshreload

提问by Martin

I would like to implement a JavaScript code which states this: if the page is loaded completely, refresh the page immediately, but only once. I'm stuck at the "only once":

我想实现一个 JavaScript 代码,它说明了这一点:如果页面完全加载,立即刷新页面,但只能刷新一次。我被困在“只有一次”:

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

this gives a loop without the "only once". jQuery is loaded if this helps.

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

回答by Cokegod

I'd say use hash, like this:

我会说使用哈希,像这样:

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

回答by Haimei

When I meet this problem, I search to here but most of answers are trying to modify existing url. Here is another answer which works for me using localStorage.

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

<script type='text/javascript'>

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

</script>

回答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 this nice solution. This works for me fine.

由于 if 条件,页面只会重新加载一次。我也遇到了这个问题,当我搜索时,我找到了这个不错的解决方案。这对我来说很好。

回答by Areeg Samir

Check this Link it contains a java-script code that you can use to refresh your page only once

检查此链接它包含一个 java 脚本代码,您只能使用该代码刷新您的页面一次

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

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

There are more than one way to refresh your page:

刷新页面的方法不止一种:

solution1:

解决方案1

To refresh a page once each time it opens use:

要在每次打开时刷新页面,请使用:

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

sollution2:

解决方案2:

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

Then change your to say

然后改变你说

<Body onLoad=" LoadOnce()" >

solution3:

解决方案3:

response.setIntHeader("Refresh", 1);

response.setIntHeader("Refresh", 1);

But this solution will refresh the page more than one time depend on the time you specifying

但是此解决方案将多次刷新页面,具体取决于您指定的时间

I hope that will help you

我希望这会帮助你

回答by mike

i put this inside my head tags of the page i want a single reload on:

我把它放在我想要一次重新加载的页面的头部标签中:

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

the value "mc" can be set to whatever you want, but both must match in the 2 lines. and the "=mobile" can be "=anythingyouwant" it just needs a value to stop the refresh.

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

回答by Mert Kadir Gürsoy

Finally, I got a solution for reloading page once after two months research.

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

It works fine on my clientside JS project.

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

I wrote a function that below reloading page only once.

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

1) First getting browser domloading time

1)首先获取浏览器domloading时间

2) Get current timestamp

2) 获取当前时间戳

3) Browser domloading time + 10 seconds

3) 浏览器 domloading 时间 + 10 秒

4) If Browser domloading time + 10 seconds bigger than current now timestamp then page is able to be refreshed via "reloadPage();"

4) 如果浏览器 domloading 时间 + 10 秒大于当前时间戳,那么页面可以通过“reloadPage();”刷新

But if it's not bigger than 10 seconds that means page is just reloaded thus It will not be reloaded repeatedly.

但如果它不大于 10 秒,则表示页面只是重新加载,因此不会重复重新加载。

5) Therefore if you call "reloadPage();" function in somewhere in your js file page will only be reloaded once.

5) 因此,如果您调用“reloadPage();” js 文件页面中某处的函数只会重新加载一次。

Hope that helps somebody

希望能帮助某人

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

回答by Vlad Webster

After </body>tag:

</body>标签:

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

回答by bakchod_launda

use this

用这个

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

回答by Darmawansyah Developer

use window.localStorage... like this

使用 window.localStorage ... 像这样

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

It's work for me

这对我有用

回答by MING WU

Here is another solution with setTimeout, not perfect, but it works:

这是 setTimeout 的另一个解决方案,并不完美,但它有效:

It requires a parameter in the current url, so just image the current url looks like this:

它需要当前 url 中的参数,因此只需对当前 url 进行图像处理,如下所示:

www.google.com?time=1

The following code make the page reload just once:

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

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

The accepted answer uses the least amount of code and is easy to understand. I just provided another solution to this.

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

Hope this helps others.

希望这对其他人有帮助。