当我转到下一个URL时,警报不起作用。我需要每3分钟获取一次有关窗口位置onchange的window href值。



var i=0;
function load(){

  var currentLoc = window.location.href;
  PrevLoc = currentLoc;
  interval(currentLoc,PrevLoc);
}

function interval(currentLoc,PrevLoc){
  setInterval(function () {

    if(currentLoc == 'https://github.com/features'){
 		  alert('Reached the Target Page')
    //  document.write('Reached the Target Page');
      return true;
    }

    if(currentLoc == PrevLoc){
      	alert(window.location.href);
    //  document.write(window.location.href);

      if(i==0){
        PrevLoc = window.location.href;
        window.location.href = "https://github.com/join?source=login";
        currentLoc=window.location.href;
      }
      interval(currentLoc,PrevLoc);
      i++;

    }
    else {
    	alert(window.location.href);
 //     document.write(window.location.href);
    }
  },3000);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">   </script>
<body onLoad="load();">
</body>





我正在尝试使用setInterval使用javascript和jquery进行此操作。但更改href值后,它不起作用。

 <html>
 <head>


 </head>
 <body onLoad="load();">

 <script>
  function load(){
    window.location.href = "https://github.com/join?source=login"
    var PrevLoc = "https://github.com/join?source=login";
    var currentLoc = window.location.href;

    if(currentLoc == PrevLoc){
       alert('inside')
       setInterval(function () {
         alert('changed')
      },3000);
    }
   else {

   }
 }
</script>
</body>
</html>

最佳答案

改变这个:

if(currentLoc == PrevLoc){
   alert('inside')
   setInterval(function () {
     alert('changed')
  },3000);
}


对此或您要检查的内容:

setInterval(function () {
  if(currentLoc != PrevLoc){
     alert('changed')
  }
},3000);

10-05 18:51