可以使用window.location对象来获取当前页面的URL,例如:

var currentUrl = window.location.href;
console.log(currentUrl);

除此之外,还可以使用浏览器提供的API来监测URL的变化,例如:

  1. 使用window.addEventListener方法监听hashchange事件,当浏览器URL的hash部分发生变化时触发。
window.addEventListener('hashchange', function() {
  console.log('URL hash changed: ' + window.location.href);
});
  1. 使用window.history.pushState方法或window.history.replaceState方法修改URL时,会触发popstate事件。
window.addEventListener('popstate', function() {
  console.log('URL changed: ' + window.location.href);
});
``
js 全部可以监测url

原文地址: https://www.cveoy.top/t/topic/dWIQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录