繁体   English   中英

检测网址更改的特定部分

[英]Detect specific part of url change

我在检测URL上的更改时遇到问题,问题是,在#之后,我使用url的相同第一部分,例如

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

如果我用window.onhashchange = function();来监听变化 ,如果我只是将网址更改为,它就不会检测到更改

 mySite.com/masterPage.html#pageView=student

由于哈希值保持与以前相同,因此有关如何检测url上pageView的更改的任何想法都可以更改

用jQuery做,对我来说很好。

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

测试了是否更改锚点时触发了mySite.fr/#toto和mySite.fr/#tototata事件。

如果您使用链接来更改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>

但是,如果直接数字输入url,则没有任何操作可以引发该事件,因此无法使用onsh​​ashchange函数。 而是使用cookie保存最后的哈希并检查它是否被更改。

再见

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM