Permalink
Browse files

Change onbeforeunload handler to only remove event listeners rather t…

…han invoking a full destroy on all elements. This prevents the flash of unstyled elements while leaving the page before the next page loads.
  • Loading branch information...
1 parent 3799059 commit cca64699510d0424a901a5ea07b608ca55cefc2b Jason Johnston committed Nov 5, 2010
Showing with 32 additions and 21 deletions.
  1. +32 −21 sources/Element.js
View
@@ -101,6 +101,8 @@ PIE.Element = (function() {
el.attachEvent( 'onmouseenter', mouseEntered );
el.attachEvent( 'onmouseleave', mouseLeft );
PIE.OnResize.observe( handleMoveOrResize );
+
+ PIE.OnBeforeUnload.observe( removeEventListeners );
}
boundsInfo.unlock();
@@ -236,13 +238,41 @@ PIE.Element = (function() {
/**
+ * Remove all event listeners from the element and any monitoried ancestors.
+ */
+ function removeEventListeners() {
+ if (eventsAttached) {
+ if( ancestors ) {
+ for( var i = 0, len = ancestors.length; i < len; i++ ) {
+ ancestors[i].detachEvent( 'onpropertychange', ancestorPropChanged );
+ ancestors[i].detachEvent( 'onmouseenter', mouseEntered );
+ ancestors[i].detachEvent( 'onmouseleave', mouseLeft );
+ }
+ }
+
+ // Remove event listeners
+ el.detachEvent( 'onmove', update );
+ el.detachEvent( 'onresize', update );
+ el.detachEvent( 'onpropertychange', propChanged );
+ el.detachEvent( 'onmouseenter', mouseEntered );
+ el.detachEvent( 'onmouseleave', mouseLeft );
+
+ PIE.OnBeforeUnload.unobserve( removeEventListeners );
+ eventsAttached = 0;
+ }
+ }
+
+
+ /**
* Clean everything up when the behavior is removed from the element, or the element
- * is destroyed.
+ * is manually destroyed.
*/
function destroy() {
if( !destroyed ) {
var i, len;
+ removeEventListeners();
+
destroyed = 1;
// destroy any active renderers
@@ -252,30 +282,14 @@ PIE.Element = (function() {
}
}
- // remove any ancestor propertychange listeners
- if( ancestors ) {
- for( i = 0, len = ancestors.length; i < len; i++ ) {
- ancestors[i].detachEvent( 'onpropertychange', ancestorPropChanged );
- ancestors[i].detachEvent( 'onmouseenter', mouseEntered );
- ancestors[i].detachEvent( 'onmouseleave', mouseLeft );
- }
- }
-
// Remove from list of polled elements in IE8
if( PIE.ie8DocMode === 8 ) {
PIE.Heartbeat.unobserve( update );
}
// Stop onresize listening
PIE.OnResize.unobserve( update );
- // Remove event listeners
- el.detachEvent( 'onmove', update );
- el.detachEvent( 'onresize', update );
- el.detachEvent( 'onpropertychange', propChanged );
- el.detachEvent( 'onmouseenter', mouseEntered );
- el.detachEvent( 'onmouseleave', mouseLeft );
-
- // Kill objects
+ // Kill references
renderers = boundsInfo = styleInfos = styleInfosArr = ancestors = el = null;
}
}
@@ -362,9 +376,6 @@ PIE.Element = (function() {
return els;
};
- // Destroy all Element objects when leaving the page
- PIE.OnBeforeUnload.observe( Element.destroyAll );
-
return Element;
})();

0 comments on commit cca6469

Please sign in to comment.