Is there work for addEventListener callbacks with return value?

I'm currently experimenting with custom (level 2 DOM) events, and now I have come addEventListener()across the problem that I will not accept callbacks that return a value, or at least I am not familiar with the correct approach to this.

Basically, I want:

    addEventListener("customEvent",
        function() {
            return true ;
         },
    false) ;

so if I create an instance of a wrapper function new wrapper(),

     function wrapper() {
         addEventListener(...) ;
     }

this will be correctly returned truewhenever an event is triggered and caught.

Please keep in mind that this is experimental: I know that there are many solutions that do not require a return from the method addEventListener. I am just wondering if there is work or if it is really a dead end, and I should not worry.

!

+3
1

AddEventListener . addEventListener , , , callbackFunction, , addEventListener, .

addEventListener( 'onload', function() {

  // do something

  return true;

  // bool(true) would be returned to window.event[onload] and not to addEventListener if that were the case which would still make it useless to you.

}, false );

, , , .

var eventTracker = {

  retVal: false,

  retEvt: false,

  trigger: function( e ) {

    e = e || window.event;

    // some code here
  }

};

function someFn(e) {

  e = e || window.event;

  // Some code here

  eventTracker.retVal = true;

  eventTracker.retEvt = e.type;

  eventTracker.trigger.call( e );

}

// Bind the event in all browsers
if ( window.addEventListener ) {
    window.addEventListener( 'load', someFn, false );
} else if ( window.attachEvent ) {
    window.attachEvent( 'onload', someFn );
} else {
    window.onload = someFn;
}
+2

Source: https://habr.com/ru/post/1740146/


All Articles