Compatible with all browsers such as IE and chrome, return to the top code, and chrome return to the top
Today, I saw a post in the blog garden back to the top of the Code, simply read it, but found that it can run in a non-IE browser, but it cannot run in IE.
After some modifications are made to the Code, it perfectly supports running in IE.
Main Implementation function code file:
<Html> Note: Event. js is mainly used to add events to and remove events from all browsers.
The Code is as follows:
/** Add an event handler * @ param object the element of the event handler * @ param string type event name, such as click * @ param function handler event handler, it can be directly given as an anonymous function or a defined function name. The event handler specified by the anonymous function method can be removed from IE6 IE7 IE8 and cannot be removed from the standard browser. * @ Param boolean remove: whether to remove an event. This parameter is written to simplify the removeEvent function and does not play any role in adding an event handler. * description: A function completely replaces attachEvent/detachEvent, and supports all mainstream browsers to solve the issue of binding the IE6 IE7 IE8 event first and then executing it. * The following functions are perfectly compatible with addEventListener/removeEventListener and attachEvent/detachEvent. They support all mainstream browsers such as Google Chrome/Firefox/Safari/Opera/IE 6 7 8 9 10 11, * this solution perfectly resolves IE6 IE7 IE8 this pointing error, and can correct the error that occurs after the events in IE6 IE7 IE8 are first bound. */Function addEvent (object, type, handler, remove) {if (typeof object! = 'Object' | typeof handler! = 'Function') return; try {object [remove? 'Removeeventlistener ': 'addeventlistener'] (type, handler, false);} catch (e) {var xc = '_' + type; object [xc] = object [xc] | []; if (remove) {var l = object [xc]. length; for (var I = 0; I <l; I ++) {if (object [xc] [I]. toString () = handler. toString () object [xc]. splice (I, 1) ;}} else {var l = object [xc]. length; var exists = false; for (var I = 0; I <l; I ++) {if (object [xc] [I]. toString () = = Handler. toString () exists = true;} if (! Exists) object [xc]. push (handler);} object ['on' + type] = function () {var l = object [xc]. length; for (var I = 0; I <l; I ++) {object [xc] [I]. apply (object, arguments) ;}}}/* remove event handler */function removeEvent (object, type, handler) {addEvent (object, type, handler, true );}View Code
If anything is wrong, I hope you can correct me. Thank you very much.
Source code: https://pan.baidu.com/s/1eR5xD2i