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

Javascript 第一次页面加载后的一次页面刷新,javascript,jquery,refresh,reload,Javascript,Jquery,Refresh,Reload,我想实现一个JavaScript代码,其中说明: 如果页面已完全加载,请立即刷新页面,但仅刷新一次。 我被困在“只有一次”: 这将生成一个没有“仅一次”的循环。如果有帮助,jQuery将被加载。我会说使用哈希,如下所示: window.onload = function() { if(!window.location.hash) { window.location = window.location + '#loaded'; window.location

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


这将生成一个没有“仅一次”的循环。如果有帮助,jQuery将被加载。

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

window.onload = function() {
    if(!window.location.hash) {
        window.location = window.location + '#loaded';
        window.location.reload();
    }
}
var refresh = window.localStorage.getItem('refresh');
console.log(refresh);
if (refresh===null){
    window.location.reload();
    window.localStorage.setItem('refresh', "1");
}
www.google.com?time=1

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

例如:
-->将刷新
-->不会刷新

如果您不想看到凌乱的URL,那么我会在正文的开头包含一些PHP,它会回显一个隐藏值,本质上说,不刷新页面所需的POST参数是否包含在初始页面请求中。在这之后,您将包含一些JS来检查该值,并在必要时使用该帖子信息刷新页面


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

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

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

试试这个

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

请尝试使用下面的代码

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

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

    return;
    }
});

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

刷新页面的方法有多种:

解决方案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:

<script language=" JavaScript" >
<!-- 
function LoadOnce() 
{ 
window.location.reload(); 
} 
//-->
</script>
response.setInHeader(“刷新”,1)

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

我希望这将对您有所帮助


<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>
$(文档).ready(函数(){ //检查当前URL是否包含“#” if(document.URL.indexOf(“#”)==-1){ //将URL设置为任意加上“#”。 url=document.url+“#”; location=“#”; //重新加载页面 位置。重新加载(true); } });
由于if条件,页面将只重新加载一次。我也遇到了这个问题,当我搜索时,我找到了这个很好的解决方案。
这对我来说很好。

当我遇到这个问题时,我会搜索到这里,但大多数答案都试图修改现有的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>

(功能()
{
if(window.localStorage)
{
如果(!localStorage.getItem('firstLoad'))
{
localStorage['firstLoad']=true;
window.location.reload();
}  
其他的
localStorage.removietem('firstLoad');
}
})();
使用rel=“外部” 下面就是一个例子

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

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

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

    window.onload = function() {
        if(!window.location.hash) {
            window.location = window.location + '#loaded';
            window.location.reload();
        }
    }
    
    var refresh = window.localStorage.getItem('refresh');
    console.log(refresh);
    if (refresh===null){
        window.location.reload();
        window.localStorage.setItem('refresh', "1");
    }
    
    www.google.com?time=1
    
    它适合我。

    标记之后:

    <script type="text/javascript">
    if (location.href.indexOf('reload')==-1)
    {
       location.href=location.href+'?reload';
    }
    </script>
    
    
    if(location.href.indexOf('reload')=-1)
    {
    location.href=location.href+'?重新加载';
    }
    
    经过两个月的研究,我终于找到了一个重新加载页面的解决方案

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

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

    1) 首次获取浏览器加载时间

    2) 获取当前时间戳

    3) 浏览器加载时间+10秒

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

    但若它不超过10秒,这意味着页面只是被重新加载,所以它不会被重复加载

    5) 因此,如果在js文件页面的某个地方调用“reloadPage();”函数,则只会重新加载一次

    希望这对某人有帮助

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

    下面是另一个使用setTimeout的解决方案,它并不完美,但可以工作:

    它需要当前url中的一个参数,因此只需对当前url进行如下映像:

    window.onload = function() {
        if(!window.location.hash) {
            window.location = window.location + '#loaded';
            window.location.reload();
        }
    }
    
    var refresh = window.localStorage.getItem('refresh');
    console.log(refresh);
    if (refresh===null){
        window.location.reload();
        window.localStorage.setItem('refresh', "1");
    }
    
    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);
               }
              }
    
    被接受的答案使用最少的代码并且容易理解。我只是提供了另一个解决方案

    希望这对其他人有帮助。

    
    
    <script>
    
    function reloadIt() {
        if (window.location.href.substr(-2) !== "?r") {
            window.location = window.location.href + "?r";
        }
    }
    
    setTimeout('reloadIt()', 1000)();
    
    </script>
    
    函数重载{ if(window.location.href.substr(-2)!=“?r”){ window.location=window.location.href+“?r”; } } setTimeout('reloadIt()',1000)();

    这非常有效

    您可以使一个页面变为可执行的
    one=false
    然后使用
    if-else
    重新加载页面,就像if
    one==false
    重新加载页面一次变为true一样。

    重新加载页面会实现什么?也许有更好的方法来解决这个问题。页面上有一个jquery全屏库脚本,但是拇指永远不会完全加载。如果你点击页面的“刷新”按钮,它只会加载剩余的页面。。。所以很明显,我正在寻找某种解决方法。。。分开给大拇指太复杂了(CMS),其他的错误修复并没有真正的帮助。。。它有4MB的图像,每个大约300kb,在某个时候图像的拇指不再被调整大小和显示(超时?太多,太大??),但是页面完成加载。我在使用pjax.yes时遇到了类似的问题(javascripts不工作)。我只是想提出这个建议。是的,类似于我的建议,使用$\u GET参数,但要简单一点,因为在JS中访问哈希比访问$\u GET参数更容易……这是最简单的,只要URL中还没有哈希。在这种情况下,读取GET参数会更好。如何在不附加url的情况下执行此操作?聪明的解决方案!爱死它了!❤创造性的解决方案。@Haimei Thx很多\uuuuuu/\\uuuu对于任何想知道的人来说,这会在不添加或修改原始URL的情况下重新加载页面一次注:1000表示1秒,您可以将其设置为您想要的任何时间间隔