Skip to content

Commit

Permalink
Use pageshow/pagehide instead of load/unload
Browse files Browse the repository at this point in the history
Browsers that use a page cache may not always fire onunload when a user
navigates away from a page but doesn't close the window.  They fire the
pagehide event instead, and the pageshow event when the user returns to
the page.

It's better for us to use these events on browsers that support it.
pageshow/pagehide also fire when onload/unload would normally have
fired.

Thanks to github user @solstice for the info in issue 43:
YahooArchive#43
and the Surfin Safari blog:
http://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
  • Loading branch information
bluesmoon committed Jun 12, 2012
1 parent b79977e commit d579368
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions boomerang.js
Expand Up @@ -249,11 +249,15 @@ boomr = {

// The developer can override onload by setting autorun to false
if(!("autorun" in config) || config.autorun !== false) {
impl.addListener(w, "load",
function() {
impl.fireEvent("page_ready");
}
);
var load_handler = function() {
impl.fireEvent("page_ready");
};
if("onpagehide" in w) {
impl.addListener(w, "pageshow", load_handler);
}
else {
impl.addListener(w, "load", load_handler);
}
}

impl.addListener(w, "DOMContentLoaded", function() { impl.fireEvent("dom_loaded"); });
Expand All @@ -270,8 +274,13 @@ boomr = {
else if(d.visibilityState)
impl.addListener(d, "visibilitychange", fire_visible);

// This must be the last one to fire
impl.addListener(w, "unload", function() { w=null; });
if(!("onpagehide" in w)) {
// This must be the last one to fire
// We only clear w on browsers that don't support onpagehide because
// those that do are new enough to not have memory leak problems of
// some older browsers
impl.addListener(w, "unload", function() { w=null; });
}

return this;
},
Expand Down Expand Up @@ -308,22 +317,21 @@ boomr = {
// support it. This allows us to fall back to onunload when onbeforeunload
// isn't implemented
if(e_name === 'page_unload') {
impl.addListener(w, "unload",
function() {
if(fn) {
fn.call(cb_scope, null, cb_data);
}
fn=cb_scope=cb_data=null;
}
);
impl.addListener(w, "beforeunload",
function() {
var unload_handler = function() {
if(fn) {
fn.call(cb_scope, null, cb_data);
}
fn=cb_scope=cb_data=null;
}
);
};
// pagehide is for iOS devices
// see http://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
if("onpagehide" in w) {
impl.addListener(w, "pagehide", unload_handler);
}
else {
impl.addListener(w, "unload", unload_handler);
impl.addListener(w, "beforeunload", unload_handler);
}
}

return this;
Expand Down

0 comments on commit d579368

Please sign in to comment.