使用h5新特性监听App自带返回键

  1. pushHistory(); 
    window.addEventListener("popstate", function(e) { 
        alert("我监听到了浏览器的返回按钮事件啦");//根据自己的需求实现自己的功能 
    }, false); 
    function pushHistory() { 
        var state = { 
            title: "title", 
            url: "#"
        }; 
        window.history.pushState(state, "title", "#"); 
    }

  2. var hiddenProperty = 'hidden' in document ? 'hidden' :    
        'webkitHidden' in document ? 'webkitHidden' :    
        'mozHidden' in document ? 'mozHidden' :    
        null;
    var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
    var onVisibilityChange = function(){
        if (!document[hiddenProperty]) {    
            console.log('页面非激活');
        }else{
            console.log('页面激活')
        }
    }
    document.addEventListener(visibilityChangeEvent, onVisibilityChange);

    https://developer.mozilla.org/zh-CN/docs/Web/API/Document/hidden

你可能感兴趣的:(前端)