编辑于:2019年
1、初始化页面监听
//初始化页面监听
document.addEventListener("DOMContentLoaded", function(){
//TODO
});
2、页面跳转hash
// 页面跳转hash
document.addEventListener("hashchange", function(){
//TODO
});
3、beforeunload
beforeunload事件当窗口将要关闭,或者document和网页资源将要卸载时触发。它可以用来防止用户不当心关闭网页。该事件的默认动作就是关闭当前窗口或文档。如果在监听函数中,调用了event.preventDefault(),或者对事件对象的returnValue属性赋予一个非空的值,就会自动跳出一个确认框,让用户确认是否关闭网页。如果用户点击“取消”按钮,网页就不会关闭。监听函数所返回的字符串,会显示在确认对话框之中:
window.addEventListener('beforeunload',function(event){
if(event.preventDefault){
event.preventDefault();
}else{
event.returnValue='你确认要离开吗?';
}
});
4、unload与load
unload事件在窗口关闭或者document对象将要卸载时触发,发生在window,body,frameset等对象上面。它的触发顺序排在beforeunload,pagehide事件后面。unload事件只在页面没有被浏览器缓存时才会触发,换言之,如果通过按下“前进/后退”导致页面