如何检查JavaScript中的URL是否已更改?例如,使用AJAX的GitHub等网站会在#符号后附加页面信息,以创建唯一的URL而无需重新加载页面.检测此URL是否更改的最佳方法是什么?
onload再次调用事件?解决方案应该是纯Javascript - 没有外部库和框架.
在SPA中,Everything使用路由机制进行路由.我只是想听到一个网址的任何部分发生变化(不仅是哈希.我想要检测的任何变化)
以下是SPA的例子,
https://www.google.in/
https://www.google.in/women
https://www.google.in/girl
Run Code Online (Sandbox Code Playgroud)
现在每当网址从https://www.hopscotch.in/更改为https://www.hopscotch.in/women时,我想捕获该事件.
我试过了,
window.addEventListener("hashchange",function(event){
console.log(this); // this gets fired only when hash changes
});
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以使用MutationObserver监视window.location.pathname(或window.location.hash)中的更改。