Another version of addevent and removeevent

Source: Internet
Author: User
Tags button type

mainly improves memory leakage

(Function () {window. eventutils = {eventfuncs: [], addevent: function (O, N, f) {var El, ID; // resolve element by ID if neededo = typeof (o) = 'string '? Document. getelementbyid (o): O; If (O. attachevent) {// since we can't use attachevent we need to generate an unique ID for the object // and place functions in an array one for each objectel = eventutils. eventfuncs; id = O. _ evtid; If (! El [ID]) {// generate new unique Idid = O. _ evtid = el. length; // Add event listener old fashion way instead of attachevento ['on' + N] = function () {var I, L, E = Window. event, libaie.tar get = E. srcelement; // force W3C style // execute each event listener in orderfor (I = 0, Li = el[e.tar get. _ evtid], L = Li. length; I <L; I ++) Li [I] (e) ;}; // create array with first functionel [ID] = [f];} elseel [ID]. push (F ); // Push in more functions // fix the IE leako = NULL;} else if (O. addeventlistener) O. addeventlistener (n, F, false); elseo ['on' + N] = f; return F ;}, removeevent: function (O, N, f) {var I, li; // resolve element by ID if neededo = typeof (o) = 'string '? Document. getelementbyid (o): O; If (O. detachevent) {li = eventutils. eventfuncs [O. _ evtid]; If (LI) {// detach event listener by looking for it and remove it from the arrayfor (I = 0; I <li. length; I ++) {If (Li [I] = f) Li. splice (I, 1) ;}}} else if (O. removeeventlistener) O. removeeventlistener (n, F, false); elseo ['on' + N] = NULL ;}};})();

<Br/> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <title> example of an simple event class not using the unload event </title> <br/> <SCRIPT type = "text/JavaScript"> <br/> (function () {<br/> window. eventutils = {<br/> eventfuncs: [], </P> <p> addevent: function (O, N, f) {<br/> var E L, ID; </P> <p> // resolve element by ID if needed <br/> O = typeof (o) = 'string '? Document. getelementbyid (o): O; </P> <p> If (O. attachevent) {<br/> // since we can't use attachevent we need to generate an unique ID for the object <br/> // and place functions in an array one for each object <br/> El = eventutils. eventfuncs; <br/> id = O. _ evtid; </P> <p> If (! El [ID]) {<br/> // generate new unique id <br/> id = O. _ evtid = el. length; </P> <p> // Add event listener old fashion way instead of attachevent <br/> O ['on' + N] = function () {<br/> var I, L, E = Window. event, Li; </P> <p> e.tar get = E. srcelement; // force W3C style </P> <p> // execute each event listener in order <br/> for (I = 0, Li = elw.e.tar get. _ evtid], L = Li. length; I <L; I ++) <br/> Li [I] (e); <br/>}; </P> <P> // create array with first function <br/> El [ID] = [f]; <br/>} else <br/> El [ID]. push (f); // push in more functions </P> <p> // fix the IE leak <br/> O = NULL; <br/>} else if (O. addeventlistener) <br/> O. addeventlistener (n, F, false); <br/> else <br/> O ['on' + N] = f; </P> <p> return F; <br/>}, </P> <p> removeevent: function (O, N, f) {<br/> var I, Li; </P> <p> // resolve element by ID if needed <br/> O = typeof (O) = 'string '? Document. getelementbyid (o): O; </P> <p> If (O. detachevent) {<br/> li = eventutils. eventfuncs [O. _ evtid]; </P> <p> If (Li) {<br/> // detach event listener by looking for it and remove it from the array <br/> for (I = 0; I <li. length; I ++) {<br/> If (Li [I] = f) <br/> Li. splice (I, 1); <br/>}< br/>} else if (O. removeeventlistener) <br/> O. removeeventlistener (n, F, false); <br/> else <br/> O ['on' + N] = NULL; <br/>}< br/>}; <br/>}) (); </P> <p> function addlisteners () {<br/> var F; </P> <p> eventutils. addevent ('elm ', 'click', function (e) {<br/> alert ('listener 1:' + e.tar get. ID); <br/>}); </P> <p> F = eventutils. addevent ('elm ', 'click', function (e) {<br/> alert ('listener 2:' + e.tar get. ID); <br/>}); </P> <p> eventutils. addevent ('elm ', 'click', function (e) {<br/> alert ('listener 3:' + e.tar get. ID); <br/> }); </P> <p> eventutils. removeevent ('elm ', 'click', F ); <br/>}< br/> </SCRIPT> <br/> </pead> <br/> <body onload = "addlisteners (); "> </P> <p> <p> example of an simple event class not using the unload event </p> </P> <p> <Div id =" elm "> click me and try me in drip or sieve! </Div> </P> <p> </body> <br/> </ptml> <br/>

RunCode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.