chrome 判断当前是关闭浏览器还是刷新浏览器

125 阅读1分钟

注:据描述ie好像不行,火狐也不执行onunload

    window.onunload = (event) => {
      this._gap_time = new Date().getTime() - this._beforeUnload_time;
      if (this._gap_time <= 5 || event.altKey) {
          // 关闭
          // 执行关闭需要进行的操作
        }
    };

    window.onbeforeunload = () => {
      this._beforeUnload_time = new Date().getTime();
    };

今天接到一个优化点,希望在关闭浏览器页的时候同时清除当前登录用户信息,就不允许再次通过url直接进到系统内。
找了很多方案,最终这个算是能够比较好的实现这个优化点的。
别问原理,原理不懂,event.altKey监听了键盘的alt事件,相当于能够处理点击关闭按钮和快捷键关闭。基本能满足了,多的不想搞了