history-back

;!function(pkg, undefined){ //此声明函数在xback.js文件里有,在app.js里必须再声明一次,不然监听返回事件失败
var STATE = 'x-back';
var element;

var onPopState = function(event){
    event.state === STATE && fire();
    record(STATE);  //初始化事件时,push一下
}

var record = function(state){
    history.pushState(state, null, location.href);
}

var fire = function(){
    var event = document.createEvent('Events');
    event.initEvent(STATE, false, false);
    element.dispatchEvent(event);
}

var listen = function(listener){
    element.addEventListener(STATE, listener, false);
}

!function(){
    element = document.createElement('span');
    window.addEventListener('popstate', onPopState);
    this.listen = listen;
    //record(STATE);  //注释掉这行代码
}.call(window[pkg] = window[pkg] || {});

}('XBack');

 

 

 

 

var STATE = "x-back"
history.pushState(STATE, null, location.href);
window.onpopstate = function(event) {
if(event.state === STATE) {
if (opt.callback && typeof opt.callback == 'function') {
opt.callback();
}
}
};

posted on 2015-12-14 20:04  哈哈李小博  阅读(217)  评论(0编辑  收藏  举报