2016-03-15 135 views
0

我有一種檢測我的網址變化問題的特定部分,問題是,我使用的URL相同的第一部分#後,例如檢測URL變化

mySite.com/masterPage.html#pageView=employee&employeeCode=10 

如果我聽與窗口的變化.onhashchange = function(); ,它不檢測更改,如果我只是改變的URL

mySite.com/masterPage.html#pageView=student 

因爲哈希一直保持和以前一樣,任何想法如何檢測的URL變化瀏覽量的變化

+0

如果你正在觸發改變,你可以不跟蹤它嗎? – Ukuser32

回答

0

與jQuery做它,工作正常,我。

$(window).on("hashchange", function(){ 
    alert('change'); 
}); 

測試蒙山mySite.fr/#toto和mySite.fr/#tototata 事件時,錨改變被解僱了。

+0

閱讀的問題,這將不會檢測#後第一部分保持不變 – noitse

+0

讀取響應,我用#toto和#tototata測試我的解決方案。 012ottototata的第一部分是toto,不是嗎? –

+0

我剛測試過#pageView = employee&employeeCode = 10和#pageView = student,再次正常工作。 –

0

如果您使用鏈接來更改URL散列,使用onhashchange沒有問題,因爲有一個操作可以更改它(錨點或腳本)。 看看這個:https://jsfiddle.net/FrancescoRombecchi/jxbq7epg/1/

<body onhashchange="myFunction()"> 

<p>Click the button to change the anchor part of the current URL to #part5</p> 

<button onclick="changePart()">Try it</button> 
<a href="#prova">Go to hello</a> 
<hr> 
<p id="demo"></p> 

<p id="hello">hello<p> 

<script> 

function changePart() { 
    location.hash = "part5"; 
    var x = location.hash; 
    document.getElementById("demo").innerHTML = "The anchor part is now: " + x; 
} 

// Alert some text if there has been changes to the anchor part 
function myFunction() { 
    alert("The anchor part has changed!"); 
} 


</script> 

</body> 

但是,如果你的數字直接鏈接沒有任何的行動,能夠起到事件,因此它不可能使用onsh​​ashchange功能。而是使用cookie來保存最後一個散列,並檢查它是否被更改。

再見