Skip to content

Instantly share code, notes, and snippets.

@lizzie
Created October 30, 2013 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizzie/7226633 to your computer and use it in GitHub Desktop.
Save lizzie/7226633 to your computer and use it in GitHub Desktop.
IE8- 下, jQuery 绑定自定义事件之后, 无法删除导致内存泄露问题
// jQuery 1.7.2- removeEvent 的代码
function( elem, type, handle ) {
if ( elem.detachEvent ) {
elem.detachEvent( "on" + type, handle );
}
};
// jQuery 1.8.0+ removeEvent 的代码
function( elem, type, handle ) {
var name = "on" + type;
if ( elem.detachEvent ) {
// #8545, #7054, preventing memory leaks for custom events in IE6-8 –
// detachEvent needed property on element, by name of that event, to properly expose it to GC
if ( typeof elem[ name ] === "undefined" ) {
elem[ name ] = null;
}
elem.detachEvent( name, handle );
}
};
// 相关 https://github.com/markelog/jquery/commit/71260802ab48e3926e56b5d2ff819e1a53d8217c
// - http://www.reigndropsfall.net/2011/01/05/internet-explorer-event-handler-leaks/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment