vue、jquery 监听浏览器回退事件,然后返回到指定页面

vue:

methods: {
    //返回按钮指定到首页去
    goBack() {
      this.$router.push({ path: "/" });
    },
  },
  //页面销毁时,取消监听
  destroyed() {
    window.removeEventListener("popstate", this.goBack, false);
  },
  mounted() {
    //监听浏览器返回
    if (window.history) {
      history.pushState(null, "", document.URL); //这里有没有都无所谓,最好是有以防万一
      window.addEventListener("popstate", this.goBack, false); // 回退时执行goback方法
    }
  },

jquery:

//处理回退
        if (window.history && window.history.pushState) {
            history.pushState(null, null, document.URL);
            window.addEventListener('popstate', function () {
                history.pushState(null, null, document.URL);
                //跳转首页
                this.window.location = $get("<%=returnUrl.ClientID %>").value;
            });
        }

 

posted @ 2022-06-22 14:27  潇潇mini  阅读(1546)  评论(0编辑  收藏  举报