H5中安卓返回键拦截

需求描述:在h5页面中弹出提示框等模态框时,点击路由回退(安卓返回键等)未关闭模态框而是路由进行了回退,此方法在监测到回退操作时进行了一次拦截并抛出一个事件进行自定义操作

拦截方法封装:

export function popstateListener(callback: Function) {
    history.pushState(null, "", location.href);
    let popstateFun = () => {
        callback();
        window.removeEventListener("popstate", popstateFun);
    }
    window.addEventListener("popstate", popstateFun);
}

使用方法:

popstateListener(() => {
     console.log("已拦截返回事件,执行自己的操作")
})

改进方法:

// 该方法当前页不是第一视图时(包含子路由已显示)务必移除,否则有未知错误
let callbacks;
let hasListenersBack = false;
let listenersBackIdentifier;
function popstateFun() {
    callbacks();
    window.removeEventListener("popstate", popstateFun);
    hasListenersBack = false;
    listenersBackIdentifier = null;
}
class PopstateHandler {
    // 页面中有多处需要注册时加上identifier区分
    popstateListener(callback: Function, identifier?: string) {
        if (!hasListenersBack) {
            hasListenersBack = true;
            callbacks = callback;
            listenersBackIdentifier = identifier;
            history.pushState(null, "", location.href);
            window.addEventListener("popstate", popstateFun);
        }
    }
    removePopstateListener(identifier?: string) {
        // 监听时未添加标识或者与监听时添加标识一样可移除
        setTimeout(() => {
            if ((listenersBackIdentifier && listenersBackIdentifier === identifier) || !identifier) {
                window.removeEventListener("popstate", popstateFun);
                hasListenersBack = false;
                listenersBackIdentifier = null;
                history.back()
            }
        });
    }
}
export const popstateHandler = new PopstateHandler();

防止监听方法重复注册,同时添加identifier标识,只有上次注册的identifier被清除后才能再次注册,防止页面中多处需要拦截造成混乱

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值