简体   繁体   中英

onpagehide event on iPad Safari

As there are some issues with onunload and onbeforeunload event on iPad Safari, I found an equivalent issue for iPad (pagehide) as listed on;

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

Now my question is; 1. Is pagehide equivelent to unload OR beforeunload events ? 2. I am using a DWR call (in simple terms , it is an AJAX call) inside this pagehide event handler as;

someObj.saveData(jsonString,{async:false});

Here saveData() is actually a Java function which does the data save.. Now please please...i do understand that it is a "BAD PRACTICE" to save data on unload events...But this has been coded existing into my app and i cannot change it..

So my question is how exactly will it behave...like will it continue to run in the background while the page is being unloaded and in case of pagehide at what point will it be called?

beforeunload and unload are fired when an active page is being destroyed, because it is navigated away from, or because the browser is being closed.

pagehide is fired when an active page is 'suspended' to eg disk.

If the browser is closed while the page is suspended, it may never receive the beforeunload or unload events.

pagehide was introduced because browser makers were trying to suspend and resume pages to cache (instead of reloading them), but the found that when they called the unload event, many pages would take destructive actions that would prevent the page from being resumed later. So they came up with pagehide as a similar, but different alternative.

i do understand that it is a "BAD PRACTICE" to save data on unload events

I'm not sure about that... It's a bad practice to make synchronous requests on unload because it would make the browser wait for the response at the moment the user is trying to close the window... But I think a send-and-forget asynchronous save request would be no problem.

So my question is how exactly will it behave...like will it continue to run in the background while the page is being unloaded and in case of pagehide at what point will it be called?

Javascript code called from beforeunload or unload is generally the last fragment of script that will run on your page before it is closed. That is why, if you need an answer from the server, a synchronous request is your only option (otherwise the response could never be handled). After the event completes, the page is destroyed. The pagehide event is similar, except that the page won't be destroyed, but instead it will be serialized and persisted somewhere, so it can be loaded and resumed later on.

From this it follows that you should not do any 'cleanup' logic like destroying objects etc in the pagehide event.

More details can be found in this blog post from the WebKit team when they were building the pagehide event: WebKit Page Cache II – The unload Event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM