[英]How do you trigger an event when the URL changes without the page reloading in javascript?
例如,如果我单击网页上的“下一步”按钮,网站会显示下一页的结果,但不会重新加载页面,而 URL 会发生变化。 如何检测 URL 中的这种变化并在新显示的页面上相应地触发事件?
单击“下一步”按钮后,您将执行获取新 URL 地址并根据新数据执行操作的操作。
例如:
nextBtn.addEventListener('click', () => {
const currentLocation = window.location;
// here you can get data from URL and do whatever you want with it
});
但您也可以使用 rxjs 之类的库从其他文件订阅 url。
例如:
file1.js
nextBtn.addEventListener('click', () => {
const currentLocation = window.location;
publisher.next(currentLocation);
});
file2.js
publisher.subscribe(location => {
// and you have your url data in another file
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.