Delayed loading function. After the first call, it will overwrite the original old function, and then call the new function again, so it will not judge the conditions to improve efficiency.
The Code is as follows:
// For non-delayed loading functions, conditional judgment is performed each call.
Function removeHandler (target, eventType, handler ){
If (target. removeEventListener ){
Target. removeEventListener (eventType, handler, false );
} Else {
Target. detachEvent ("on" + eventType, handler );
}
}
// Delayed loading function. After the first call, it will overwrite the original old function, and then call the new function again, so it will not judge the condition to improve efficiency.
Function addHandler (target, eventType, handler ){
If (target. addEventListener ){
AddHandler = function (target, eventType, handler ){
Target. addEventListener (eventType, handler, false );
}
} Else {
AddHandler = function (target, eventType, handler ){
Target. attachEvent ("on" + eventType, handler );
}
}
AddHandler (target, eventType, handler );
}
// Conditional Preload
// Make sure that all functions are called at the same time through conditional pre-loading. The cost is to check when the script is loaded. Pre-loading is suitable for scenarios where a function will be used immediately and is frequently used throughout the page lifecycle.
Var addEventHandler = document. body. addEventListener? Function (target, eventType, handler ){
Target. addEventListener (eventType, handler, false );
}: Function (target, eventType, handler ){
Target. attachEvent ("on" + eventType, handler );
}