1, onbeforeunload Event:
Description: Currently the three major browsers Firefox and IE support the onbeforeunload event, Opera has not yet supported.
Usage:
Object.onbeforeunload = Handler
<element onbeforeunload = "Handler" ... ></element>
Describe:
When the event is triggered, a confirmation and Cancellation dialog box is displayed, the page is determined to leave, and the cancellation continues on this page. Handler can set a return value as the display text for the dialog box.
Trigger on:
• Close browser window
• When you go to other pages via the Address bar or favorite folder
• Click Back, Forward, refresh, homepage one of the time
• Click on a URL to connect to another page
• Call 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 to the page you want to open.
• Re-assign the value of Location.href.
• Submit 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 refresh the page, found on the ipad onbeforeunload function on the ipad around, not running???
Helpless under, found originally on iOS, have their own onshow and Onhide methods//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 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 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 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, write a way to determine if the iOS system is 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;
}
Equal to this:
var nav = window.navigator.userAgent.toLowerCase ();
var ipad = nav.indexof ("ipad");
My understanding is that:
Load the listener to Windows 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 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 execute the Myunloadhandler function (equivalent to the onbeforeunload function of JS),
Then, in the Myloadhandler function, you make the event that triggers when you load the page, and in the Myunloadhandler function, the event that triggers when you leave the page.
All code and redundant code are as follows: no cuts.
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 a 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);
} */
}