拦截浏览器路由的方法---拦截history

// 拦截history的方法,因为pushState和replaceState方法并不会触发onpopstate事件,
      const originalPushState = window.history.pushState;
      const originalReplaceState = window.history.replaceState;
      window.history.pushState = function (state, title, url) {
         originalPushState.apply(this, arguments);
         console.log("拦截1",location.pathname)

      };
      window.history.replaceState = function (state, title, url) {
        originalReplaceState.apply(this, arguments);
        console.log("拦截2",location.pathname)
      };

      //回退拦截
      window.addEventListener('popstate',function () {
        console.log("拦截2",location.pathname)
      })
发布了80 篇原创文章 · 获赞 5 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_28473733/article/details/103176098