浏览器后退(返回)事件捕获

有时候碰到用户喜欢点浏览器后退(返回上一页)按钮,有时候会报错,或者数据不刷新,我们可以捕获它自定义做些事情

拿好不谢

 1 window.onload = function () {
 2     if (typeof history.pushState === "function") {
 3         history.pushState("jibberish", null, null);
 4         window.onpopstate = function () {
 5             history.pushState('newjibberish', null, null);
 6             
 7             //do your code
 8              9         };
10     }
11     else {
12         var ignoreHashChange = true;
13         window.onhashchange = function () {
14             if (!ignoreHashChange) {
15                 ignoreHashChange = true;
16                 window.location.hash = Math.random();
17             }
18             else {
19                 ignoreHashChange = false;   
20             }
21         };
22     }
23 }

 

posted @ 2016-03-29 19:07  yijinc  阅读(1778)  评论(0编辑  收藏  举报