2017-06-29 12 views
1

次のURLに行くと、アラートは機能しませんでした。ウィンドウの位置onchangeで3minsごとにウィンドウのhref値を取得する必要があります。3分ごとにウィンドウの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'){ 
 
    \t \t alert('Reached the Target Page') 
 
    // document.write('Reached the Target Page'); 
 
     return true; 
 
    } 
 

 
    if(currentLoc == PrevLoc){ 
 
     \t 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 { 
 
    \t 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>

私には、JavaScriptとjQueryのsetIntervalを使用して、これをしようとしています。 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> 
+0

window.location.href。 – Arun

答えて

0

この変更:このまたはこれまでに何を確認したいと

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

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

このセクションはonloadイベントで使用されましたが、href値が変更されたときにsetTimeout関数が呼び出されませんでした。 – Charu

0

は、それはあなたを助けることを願っていたコードの下に試してみてください。

HTML

<body onload="load();"> 

Javascriptのコードが呼び出されるウィンドウでURLを開きます

<script type="text/javascript"> 
    function load(){ 
     var PrevLoc = "https://github.com/join?source=login"; 
     var currentLoc = window.location.href; 
     setInterval(function() { 
      if(currentLoc != PrevLoc){ 
       alert('changed'); 
      } 
      else { 
       // Do something else... 
      } 
     },3000); 
    } 
</script> 
+0

初回のみ動作します。私はwindow.hrefを変更したときに関数が呼び出さなかった。しかし、私は新しいhref値が到着しても警告を発する必要があります。 – Charu

+0

ウェブページのURLを変更できますか? –

+0

はい。私は変更時にウィンドウhrefを取得する必要があります。 – Charu

関連する問題