Skip to content
Browse files

Switch from using onbeforeunload to using onunload, to prevent destru…

…ction when not actually leaving the page, e.g. when a javascript: link is clicked. Remove the onunload listener and the global window.PIE reference so they don't leak. Quit keeping a list of watched ancestors since we don't need it anymore.
  • Loading branch information...
1 parent 4aa78d7 commit 067a9b31ac11234082fba782a736f9d852324c2c Jason Johnston committed Aug 20, 2011
Showing with 33 additions and 26 deletions.
  1. +4 −6 sources/Element.js
  2. +0 −15 sources/OnBeforeUnload.js
  3. +1 −1 sources/OnMouseup.js
  4. +2 −2 sources/OnPrint.js
  5. +1 −1 sources/OnResize.js
  6. +1 −1 sources/OnScroll.js
  7. +24 −0 sources/OnUnload.js
View
10 sources/Element.js
@@ -51,7 +51,6 @@ PIE.Element = (function() {
boundsInfo = new PIE.BoundsInfo( el ),
styleInfos,
styleInfosArr,
- ancestors,
initializing,
initialized,
eventsAttached,
@@ -176,7 +175,7 @@ PIE.Element = (function() {
}
PIE.OnResize.observe( handleMoveOrResize );
- PIE.OnBeforeUnload.observe( removeEventListeners );
+ PIE.OnUnload.observe( removeEventListeners );
}
boundsInfo.unlock();
@@ -379,8 +378,9 @@ PIE.Element = (function() {
listener[ 0 ].detachEvent( listener[ 1 ], listener[ 2 ] );
}
- PIE.OnBeforeUnload.unobserve( removeEventListeners );
+ PIE.OnUnload.unobserve( removeEventListeners );
eventsAttached = 0;
+ eventListeners = [];
}
}
@@ -413,7 +413,7 @@ PIE.Element = (function() {
PIE.OnResize.unobserve( update );
// Kill references
- renderers = boundsInfo = styleInfos = styleInfosArr = ancestors = el = null;
+ renderers = boundsInfo = styleInfos = styleInfosArr = el = null;
}
}
@@ -427,12 +427,10 @@ PIE.Element = (function() {
var watch = el.currentStyle.getAttribute( PIE.CSS_PREFIX + 'watch-ancestors' ),
i, a;
if( watch ) {
- ancestors = [];
watch = parseInt( watch, 10 );
i = 0;
a = el.parentNode;
while( a && ( watch === 'NaN' || i++ < watch ) ) {
- ancestors.push( a );
addListener( a, 'onpropertychange', ancestorPropChanged );
addListener( a, 'onmouseenter', mouseEntered );
addListener( a, 'onmouseleave', mouseLeft );
View
15 sources/OnBeforeUnload.js
@@ -1,15 +0,0 @@
-/**
- * Create an observable listener for the onbeforeunload event
- */
-PIE.OnBeforeUnload = new PIE.Observable();
-window.attachEvent( 'onbeforeunload', function() { PIE.OnBeforeUnload.fire(); } );
-
-/**
- * Attach an event which automatically gets detached onbeforeunload
- */
-PIE.OnBeforeUnload.attachManagedEvent = function( target, name, handler ) {
- target.attachEvent( name, handler );
- this.observe( function() {
- target.detachEvent( name, handler );
- } );
-};
View
2 sources/OnMouseup.js
@@ -3,4 +3,4 @@
*/
PIE.OnMouseup = new PIE.Observable();
-PIE.OnBeforeUnload.attachManagedEvent( doc, 'onmouseup', function() { PIE.OnMouseup.fire(); } );
+PIE.OnUnload.attachManagedEvent( doc, 'onmouseup', function() { PIE.OnMouseup.fire(); } );
View
4 sources/OnPrint.js
@@ -19,7 +19,7 @@
}
}
- PIE.OnBeforeUnload.attachManagedEvent( window, 'onbeforeprint', beforePrint );
- PIE.OnBeforeUnload.attachManagedEvent( window, 'onafterprint', afterPrint );
+ PIE.OnUnload.attachManagedEvent( window, 'onbeforeprint', beforePrint );
+ PIE.OnUnload.attachManagedEvent( window, 'onafterprint', afterPrint );
})();
View
2 sources/OnResize.js
@@ -3,4 +3,4 @@
*/
PIE.OnResize = new PIE.Observable();
-PIE.OnBeforeUnload.attachManagedEvent( window, 'onresize', function() { PIE.OnResize.fire(); } );
+PIE.OnUnload.attachManagedEvent( window, 'onresize', function() { PIE.OnResize.fire(); } );
View
2 sources/OnScroll.js
@@ -9,7 +9,7 @@
PIE.OnScroll.fire();
}
- PIE.OnBeforeUnload.attachManagedEvent( window, 'onscroll', scrolled );
+ PIE.OnUnload.attachManagedEvent( window, 'onscroll', scrolled );
PIE.OnResize.observe( scrolled );
})();
View
24 sources/OnUnload.js
@@ -0,0 +1,24 @@
+/**
+ * Create an observable listener for the onunload event
+ */
+(function() {
+ PIE.OnUnload = new PIE.Observable();
+
+ function handleUnload() {
+ PIE.OnUnload.fire();
+ window.detachEvent( 'onunload', handleUnload );
+ window[ 'PIE' ] = null;
+ }
+
+ window.attachEvent( 'onunload', handleUnload );
+
+ /**
+ * Attach an event which automatically gets detached onunload
+ */
+ PIE.OnUnload.attachManagedEvent = function( target, name, handler ) {
+ target.attachEvent( name, handler );
+ this.observe( function() {
+ target.detachEvent( name, handler );
+ } );
+ };
+})()

0 comments on commit 067a9b3

Please sign in to comment.
Something went wrong with that request. Please try again.