有时 后 popstate 无法使用
下面的代码可以解决...
window.addEventListener("popstate", function(e) { //回调函数中实现需要的功能
//抛出异常.无法执行
//window.history.pushState(null, 'title', '__MODULE__/gerenzhongxin')
location.href = '__MODULE__/gerenzhongxin'; //在这里指定其返回的地址
}, false);
(function() {
if(!window.history.pushState || !document.dispatchEvent) return;
var href = location.href;
var flag = true;
var voidFn = function() {};
var fn = voidFn;
var dispatchFn = function() {
var evt = document.createEvent('Event');
evt.initEvent('popstate', true, true);
window.dispatchEvent(evt);
};
// window.addEventListener('load', function() {
if(location.hash !== '#flag') {
history.replaceState({}, '', '/test02.html');
if(href.search('#') === -1) {
history.pushState({}, '', href + '#flag');
} else {
history.pushState({}, '', href.replace(/#.*/, '#flag'));
}
}
/* window.addEventListener('popstate', function() {
dispatchFn = voidFn;
flag = !flag;
if(flag) {
location.reload();
}
// }, false);
setTimeout(function() {
fn = dispatchFn;
fn();
}, 20);
}, false);*/
})();