onBeforeUnload running JavaScript on iOS

Source: Internet
Author: User
Tags unique id sessionstorage

1, onbeforeunload Event:


Note: At present, the three major browsers Firefox and IE support the onbeforeunload event, Opera has not yet supported.


Usage:


Object.onbeforeunload = Handler


<element onbeforeunload = "Handler" ... ></element>


Description:

The
event triggers a confirmation and Cancellation dialog box, determines to leave the page, and cancels to stay on this page. Handler can set a return value as the display text for the dialog box.


triggered by:


closes the browser window


to other pages via the address bar or Favorites


Click Back, Forward, refresh, homepage One of the time


Click a URL to another page to connect the time


calls any of the following events: Click,document write,document open,document close,window Close, window navigate, window Navigateandfind,location replace,location Reload,form Submit.


When you open a page with window open and pass the name of the window of this page to the page you want to open.


The value of Location.href is reset.


submits a form with the specified action through the input type= "submit" button.


can be used in the following elements:


· Body, FRAMESET, window


Platform Support:


Ie4+/win, Mozilla 1.7a+, Netscape 7.2+, firefox0.9+








today in the project, the team leader asked me to use the ipad to test the previous offline cache, back not to refresh the page, found on the ipad onbeforeunload function on the ipad around, not running???





helpless, found originally in iOS, have their own onshow and Onhide method//Search from: http://www.cnblogs.com/vaal-water/archive/2012/09/25/2701769.html





the original text is as follows:





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 are coming out of the page Cache.


Make sure to isn't perform the "one-time work" that we ' d normally does in the onload handler.


            ...





return;


        }





//This are either a load event for older browsers,


//or a pageshow event for the initial load in supported browsers.


//It ' s safe to do everything I old Load event handler did here.


        ...


    }





function Myunloadhandler (evt)


    {


if (evt.persisted) {


//This is actually a pagehide event and the page are going into the page Cache.


//Make sure that we don ' t does any destructive work, or work this shouldn ' t be duplicated.


            ...





return;


        }





//This is either a unload event for older browsers,


//or a Pagehide event for page Tear-down in supported browsers.


It's safe to do everything I-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);


    }


Original http://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/





my own program is as follows:


first wrote a way to determine if the iOS system was not:


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;


}


equals this:


var nav = window.navigator.userAgent.toLowerCase ();


var ipad = nav.indexof ("ipad");





My understanding is:


load the listener to the window when the page is finished loading


Window.addeventlistener ("Pagehide", Myunloadhandler, false);


Window.addeventlistener ("Pageshow", Myloadhandler, false);





system automatically identifies the iOS system and then makes a deal


if (!parent.isios ()) {}//or if (ipad!=-1) {}





when the iOS system is loaded, execute the Myloadhandler function (equivalent to the onload function of JS), when leaving the page to perform the Myunloadhandler function (equivalent to the onbeforeunload function of JS),


then make the event that triggers when you load the page in the Myloadhandler function, and make the event that triggers when you leave the page in the Myunloadhandler function








all code and redundant code as follows: No exclusions


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); Because the ID of the div placed in the sessionstorage is the same, so the time stamp as the unique ID


$ ("#tokenDiv"). HTML (TOKENDIV);


    } */





function Myunloadhandler (evt)


    {


Alert ("Myunloadhandler");


alert ("away from the page");


var tokendiv = $ ("#tokenDiv"). html ();


Sessionstorage.setitem ("Tokendiv" +token,tokendiv);


Sessionstorage.setitem ("Start" +token,start);


Sessionstorage.setitem ("Paramflag" +token,paramflag);


    }


}


window.onbeforeunload = function () {


    


alert ("away from the page");


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);


    } */


    }

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.