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

发布于 2024-11-28 12:19:08 字数 198 浏览 2 评论 0原文

我想实现一段 JavaScript 代码,其中指出: 如果页面已完全加载,则立即刷新页面,但仅刷新一次。 我陷入了“仅一次”:

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

这给出了一个没有“仅一次”的循环。如果这有帮助的话,就会加载 jQuery。

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

妖妓 2024-12-05 12:19:08

我会说使用哈希,如下所示:

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

I'd say use hash, like this:

window.onload = function() {
    if(!window.location.hash) {
        window.location = window.location + '#loaded';
        window.location.reload();
    }
}
许久 2024-12-05 12:19:08

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

<script type='text/javascript'>

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

</script>

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.

<script type='text/javascript'>

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

</script>
眸中客 2024-12-05 12:19:08
<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 条件,页面只会重新加载一次。我也遇到了这个问题,当我搜索时,我找到了这个很好的解决方案。
这对我来说很好用。

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

人疚 2024-12-05 12:19:08

检查此链接,它包含一个 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>

解决方案2:

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

然后更改为

<Body onLoad=" LoadOnce()" >

解决方案3: >

response.setIntHeader("Refresh", 1);

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

我希望这对您有帮助

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

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:

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:

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

Then change your to say

<Body onLoad=" LoadOnce()" >

solution3:

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

尘曦 2024-12-05 12:19:08
<script>

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

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

</script>

这很好用

<script>

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

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

</script>

this works perfectly

带上头具痛哭 2024-12-05 12:19:08

经过两个月的研究,终于找到了重新加载一次页面的解决方案。

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

我编写了一个函数,该函数仅重新加载页面一次。

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

2) 获取当前时间戳

3) 浏览器 domloading 时间 + 10 秒

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

但如果不超过 10 秒,则意味着页面刚刚重新加载,因此不会重复重新加载。

5)因此,如果您调用“reloadPage();” 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();

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

It works fine on my clientside JS project.

I wrote a function that below reloading page only once.

1) First getting browser domloading time

2) Get current timestamp

3) Browser domloading time + 10 seconds

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

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

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

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();
烦人精 2024-12-05 12:19:08

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

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

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

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.

染火枫林 2024-12-05 12:19:08

像这样使用 window.localStorage...:

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

它对我有用。

Use window.localStorage... like this:

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

It works for me.

谁的新欢旧爱 2024-12-05 12:19:08

标记之后:

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

After </body> tag:

<script type="text/javascript">
if (location.href.indexOf('reload')==-1)
{
   location.href=location.href+'?reload';
}
</script>
吃颗糖壮壮胆 2024-12-05 12:19:08

我从这里得到了答案并对其进行了修改。这对我来说是完美的解决方案。

var refresh = window.localStorage.getItem('refresh');
console.log(refresh);
setTimeout(function() {
if (refresh===null){
    window.location.reload();
    window.localStorage.setItem('refresh', "1");
}
}, 1500); // 1500 milliseconds = 1.5 seconds

setTimeout(function() {
localStorage.removeItem('refresh')
}, 1700); // 1700 milliseconds = 1.7 seconds

I got the Answer from here and modified it.This is the perfect solution for me.

var refresh = window.localStorage.getItem('refresh');
console.log(refresh);
setTimeout(function() {
if (refresh===null){
    window.location.reload();
    window.localStorage.setItem('refresh', "1");
}
}, 1500); // 1500 milliseconds = 1.5 seconds

setTimeout(function() {
localStorage.removeItem('refresh')
}, 1700); // 1700 milliseconds = 1.7 seconds

水溶 2024-12-05 12:19:08

您可以创建一个可验证的 once = false,然后使用 if else 重新加载页面,例如 if once == false 重新加载页面并 make Once true。

You can make one verable once = false then reload your page with if else like if once == false reload page an make once true.

柠檬心 2024-12-05 12:19:08

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

例如:
http://example.com --> 将刷新
,则不会刷新

http://example.com?refresh=no -->如果不刷新 如果我不想要混乱的 URL,那么我会在正文的开头包含一些 PHP,它会回显一个隐藏值,该值本质上说明初始页面请求中是否包含用于不刷新页面的必要 POST 参数。之后,您将包含一些 JS 来检查该值,并在必要时使用该 POST 信息刷新页面。

You'd need to use either GET or POST information. GET would be simplest. Your JS would check the URL, if a certain param wasn't found, it wouldn't just refresh the page, but rather send the user to a "different" url, which would be the same URL but with the GET parameter in it.

For example:
http://example.com -->will refresh
http://example.com?refresh=no -->won't refresh

If you don't want the messy URL, then I'd include some PHP right at the beginning of the body that echos a hidden value that essentitally says whether the necessary POST param for not refreshing the page was included in the initial page request. Right after that, you'd include some JS to check that value and refresh the page WITH that POST information if necessary.

缪败 2024-12-05 12:19:08

尝试用这个

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

Try with this

var element = document.getElementById('position');        
element.scrollIntoView(true);`
む无字情书 2024-12-05 12:19:08

请尝试使用下面的代码

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

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

    return;
    }
});

Please try with the code below

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

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

    return;
    }
});
热血少△年 2024-12-05 12:19:08

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

它需要当前 url 中的一个参数,因此只需将当前 url 想象成这样:

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

接受的答案使用最少的数量的代码并且很容易理解。我刚刚为此提供了另一个解决方案。

希望这对其他人有帮助。

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

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

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.

瑶笙 2024-12-05 12:19:08

React Hook 对我有用。

从 'react' 导入 { useEffect, useState };

  const [load, setLoad] = useState(false);

  window.onload = function pageLoad() {
      if (load) {
        window.location.reload(true);
        setLoad(false);
      }
    };

React Hook worked for me.

import { useEffect, useState } from 'react';

  const [load, setLoad] = useState(false);

  window.onload = function pageLoad() {
      if (load) {
        window.location.reload(true);
        setLoad(false);
      }
    };
云巢 2024-12-05 12:19:08

除了这个之外,没有什么对我来说是完美的,-添加到我的 JavaScript 文件中-:

function LoadOnce() {
if (localStorage.getItem('executed') == 'false') {
window.location.reload()
localStorage.setItem('executed', true)
}
}

setTimeout(function () {
LoadOnce()
}, 100)

并在上一页中我写道:

localStorage.setItem('executed', false)

nothing work for me perfectly except this, -added to my JavaScript file-:

function LoadOnce() {
if (localStorage.getItem('executed') == 'false') {
window.location.reload()
localStorage.setItem('executed', true)
}
}

setTimeout(function () {
LoadOnce()
}, 100)

and in the previous page I wrote:

localStorage.setItem('executed', false)
可是我不能没有你 2024-12-05 12:19:08
        var foo = true;
        if (foo){
            window.location.reload(true);
            foo = false;

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

        }
感悟人生的甜 2024-12-05 12:19:08

用这个

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

use this

<body onload =  "if (location.search.length < 1){window.location.reload()}">
烟雨凡馨 2024-12-05 12:19:08

使用 rel="外部"
就像下面的例子

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

Use rel="external"
like below is the example

<li><a href="index.html" rel="external" data-theme="c">Home</a></li>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文