JavaScript's onbeforeunload function runs on iOS

Source: Internet
Author: User

Today in the project, the team leader let me use the ipad to test the previous write offline cache, back does not refresh the page, found on the ipad onbeforeunload function on the ipad around, not run???

Helpless under, found original on 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 reads 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 P            Age was coming out of the Page Cache.            Make sure to not perform the "one-time work", that we ' d normally does in the onload handler.        ... return; }//This was either a load event for older browsers,//or a pageshow event for the initial load in Supporte        D browsers.        It's safe to do everything my old Load event handler does here.    ...    } function Myunloadhandler (evt) {if (evt.persisted) {//This is actually a pagehide event and the Pag            E is going into the Page Cache.            Make sure that we don ' t does any destructive work, or work that shouldn ' t be duplicated.        ... return; }//This was either an unload event for older browsers,//or a Pagehide event for page Tear-down in support        Ed browsers. It ' s safe to do everything my Old Unload event handler do 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 write a method to determine whether the iOS system:
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 listeners to window when the page is finished loading
Window.addeventlistener ("Pagehide", Myunloadhandler, false);
Window.addeventlistener ("Pageshow", Myloadhandler, false);

The system automatically identifies the iOS system and then makes the processing
if (!parent.isios ()) {}//or if (ipad! =-1) {}

When the iOS system loads, execute the Myloadhandler function (equivalent to JS's onload function), when leaving the page to execute the Myunloadhandler function (equivalent to JS onbeforeunload function),
Then, in the Myloadhandler function, make the event that is triggered when you load the page, and in the Myunloadhandler function, the event that is triggered when you leave the page.


All the code and the redundancy code are as follows: no deletions.
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, the timestamp is the unique identity ID
$ ("#tokenDiv"). HTML (TOKENDIV);
} */

function Myunloadhandler (EVT)
{
Alert ("Myunloadhandler");
Alert ("Leave 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 ("Leave 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 ("both");
Window.addeventlistener ("Load", Myloadhandler, false);
Window.addeventlistener ("Unload", Myunloadhandler, false);
} */
}



JavaScript's onbeforeunload function runs on iOS

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.