广告:Fundebug错误监控插件,及时发现Bug,提高Debug效率!

页面

//初始化页面监听
document.addEventListener("DOMContentLoaded", ready);

// 页面跳转hash
document.addEventListener("hashchange", navigation);
// 监听pop和push需要自定义
document.addEventListener("popstate", navigation);
document.addEventListener("pushState", navigation);

//离开页面监听
document.addEventListener("beforeunload", leave);

自定义监听popstate和pushState

history.pushState = this.resetHistory("pushState");
history.replaceState = this.resetHistory("replaceState");

  resetHistory(type) {
    let orig = history[type];
    return function() {
      let rv = orig.apply(this, arguments);
      let e = new Event(type);
      e.arguments = arguments;
      window.dispatchEvent(e);
      return rv;
    };
  }

error

window.onerror = function (errorMsg, url, lineNumber) {
               alert(errorMsg + lineNumber);//错误信息+lineNumber
       };
       
window.addEventListener('unhandledrejection', event => 
    { 
       console.log('unhandledrejection:' + event);//打印event查看所有报错信息
    });      
       

yujs
42 声望3 粉丝

人体雕刻家,码农。