Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
想要在JavaScript中捕获事件吗_Javascript - Fatal编程技术网

想要在JavaScript中捕获事件吗

想要在JavaScript中捕获事件吗,javascript,Javascript,在SPA中,所有内容都使用路由机制进行路由。我只想在url的任何部分发生更改时(不仅仅是散列,还有任何更改)听一个事件 以下是SPA的示例 https://www.google.in/ https://www.google.in/category 现在每当url从https://cherrycrumble.com/至https://cherrycrumble.com/en/girls-dresses.html,我想捕获该事件 我试过了 window.addEventListener("has

在SPA中,所有内容都使用路由机制进行路由。我只想在url的任何部分发生更改时(不仅仅是散列,还有任何更改)听一个事件

以下是SPA的示例

https://www.google.in/
https://www.google.in/category 
现在每当url从
https://cherrycrumble.com/
https://cherrycrumble.com/en/girls-dresses.html
,我想捕获该事件

我试过了

window.addEventListener("hashchange",function(event){
    console.log(this); // this gets fired only when hash changes
});

您是否尝试过
popstate

window.addEventListener('popstate', function(e){console.log('url changed')});

只有在用户触发url更改时(例如,使用
onpopstate
返回)才有可能。无法检测程序更改或页面重新加载