3 回答
一瞬儿光
TA贡献178条经验 获得超70个赞
function getPage() { var hidden, state, visibilityChange; if (typeof document.hidden !== "undefined") { hidden = "hidden"; visibilityChange = "visibilitychange"; state = "visibilityState"; } else if (typeof document.mozHidden !== "undefined") { hidden = "mozHidden"; visibilityChange = "mozvisibilitychange"; state = "mozVisibilityState"; } else if (typeof document.msHidden !== "undefined") { hidden = "msHidden"; visibilityChange = "msvisibilitychange"; state = "msVisibilityState"; } else if (typeof document.webkitHidden !== "undefined") { hidden = "webkitHidden"; visibilityChange = "webkitvisibilitychange"; state = "webkitVisibilityState"; } return { 'hidden': hidden, 'visibilityChange': visibilityChange, 'state': state }; } var Page= getPage(); document.addEventListener(Page['visibilityChange'], function() { if(document[Page['state']] == 'hidden') { //关闭 } else { //未关闭 } }, false);
一瞬儿光
TA贡献178条经验 获得超70个赞
回退和关闭时两个需求,都可以监听。
后退:
window.addEventListener("popstate",function(){ //dosthing... });
退出:
document.addEventListener(Page['visibilityChange'], function() { if(document[Page['state']] == 'hidden') { //关闭 } else { //未关闭 } }, false);
添加回答
举报
0/150
提交
取消