浏览器常用监听事件

广告: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查看所有报错信息
});

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理