javascript的onbeforeunload函数在IOS上运行

时间:2022-05-15 11:11:54

今天在做项目的时候,组长让我用iPad测试一下前面写的离线缓存,后退不刷新页面,发现在iPad上onbeforeunload函数在iPad上一带而过,不运行???

无奈之下,发现原来在IOS上,有自己的onshow和onhide方法  //搜索来自:http://www.cnblogs.com/vaal-water/archive/2012/09/25/2701769.html

原文如下:

 window.addEventListener("pageshow", myLoadHandler, false);
window.addEventListener("pagehide", myUnloadHandler, false);
    function myLoadHandler(evt)
{
if (evt.persisted) {
// This is actually a pageshow event and the page is coming out of the Page Cache.
// Make sure to not perform the "one-time work" that we'd normally do in the onload handler.
... return;
} // This is either a load event for older browsers,
// or a pageshow event for the initial load in supported browsers.
// It's safe to do everything my old load event handler did here.
...
} function myUnloadHandler(evt)
{
if (evt.persisted) {
// This is actually a pagehide event and the page is going into the Page Cache.
// Make sure that we don't do any destructive work, or work that shouldn't be duplicated.
... return;
} // This is either an unload event for older browsers,
// or a pagehide event for page tear-down in supported browsers.
// It's safe to do everything my old unload event handler did here.
...
} if ("onpagehide" in window) {
window.addEventListener("pageshow", myLoadHandler, false);
window.addEventListener("pagehide", myUnloadHandler, false);
} else {
window.addEventListener("load", myLoadHandler, false);
window.addEventListener("unload", myUnloadHandler, false);
}
原文 http://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/ 我自己的程序如下:
先写了一个判断是不是IOS系统的方法:
function isIOS() {
    var userAgentInfo = navigator.userAgent;
    var Agents = [ "iPhone","iPad"];
    var flag = false;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = true;
            break;
        }
    }
    return flag;
}
等于这个:
var nav = window.navigator.userAgent.toLowerCase();
var ipad = nav.indexOf("ipad"); 我的理解是:
当页面加载完的时候给window加载监听器
 window.addEventListener("pagehide", myUnloadHandler, false);
 window.addEventListener("pageshow", myLoadHandler, false); 系统自动识别是不是IOS系统,然后做出处理
if(!parent.isIOS()){} //或者是if(ipad != -1){} 当IOS系统加载的时候,执行myloadHandler函数(相当于js的onload函数),当离开页面的时候执行myUnloadHandler函数(相当于js的onbeforeunload函数),
然后在myloadHandler函数中做出你要执行的加载页面时触发的事件,在myUnloadHandler函数中做出你要离开页面时触发的事件 全部代码以及冗余代码如下:没有删减
var nav = window.navigator.userAgent.toLowerCase();
alert(nav);
var ipad = nav.indexOf("ipad");
alert(parent.isIOS());
if(!parent.isIOS()){
    alert("ipad");
    alert("SUCCESS");
 window.addEventListener("pagehide", myUnloadHandler, false);
 /* window.addEventListener("pageshow", myLoadHandler, false);
    function myLoadHandler(evt)
    {
        alert("myLoadHandler");
        start = parseInt(sessionStorage.getItem("start"+token));
        paramFlag = sessionStorage.getItem("paramFlag"+token);
        tokenDiv = sessionStorage.getItem("tokenDiv"+token);
        //$("#tokenDiv").html(tokenDiv);   由于放在sessionstorage中的div的id都一样,故吧时间戳作为唯一标识ID
        $("#tokenDiv").html(tokenDiv);
    } */     function myUnloadHandler(evt)
    {
        alert("myUnloadHandler");
        alert("离开页面了");
    var tokenDiv = $("#tokenDiv").html();
    sessionStorage.setItem("tokenDiv"+token,tokenDiv);
    sessionStorage.setItem("start"+token,start);
    sessionStorage.setItem("paramFlag"+token,paramFlag);
    }
}
window.onbeforeunload = function(){
    
    alert("离开页面了");
    var tokenDiv = $("#tokenDiv").html();
    sessionStorage.setItem("tokenDiv"+token,tokenDiv);
    sessionStorage.setItem("start"+token,start);
    sessionStorage.setItem("paramFlag"+token,paramFlag);
   /*  if ("onpagehide" in window) {
        alert("one");
        window.addEventListener("pageshow", myLoadHandler, false);
        window.addEventListener("pagehide", myUnloadHandler, false);
    } else {
        alert("two");
        window.addEventListener("load", myLoadHandler, false);
        window.addEventListener("unload", myUnloadHandler, false);
    } */
    }