MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla
Your Search Results

    navigator.mozApps.mgmt.removeEventListener Redirect 1

    Removes the event listener previously registered with EventTarget.addEventListener().

    Syntax

    target.removeEventListener(type, listener[, useCapture])
    type
    A string representing the event type to remove.
    listener
    The EventListener function to remove from the event target.
    useCapture Optional
    Specifies whether the EventListener to be removed was registered as a capturing listener or not. If this parameter is absent, a default value of false is assumed.
    If a listener was registered twice, one with capture and one without, each must be removed separately. Removal of a capturing listener does not affect a non-capturing version of the same listener, and vice versa.
    Note: useCapture was required in most major browsers' early versions. If broad compatibility is desired, you should always provide the useCapture parameter.

    Notes

    If an EventListener is removed from an EventTarget while it is processing an event, it will not be triggered by the current actions. An EventListener will not be invoked for the event it was registered for after being removed, however it can be reattached.

    Calling removeEventListener() with arguments that do not identify any currently registered EventListener on the EventTarget has no effect.

    Example

    This is an example of adding and then removing an event listener.

    var div = document.getElementById('div');
    var listener = function (event) {
      /* do something here */
    };
    div.addEventListener('click', listener, false);
    div.removeEventListener('click', listener, false);
    

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
    Basic support 1.0 1.0 (1.7 or earlier) 9.0 7 1.0
    useCapture made optional  (Yes) 6.0 9.0 (12.00)  (Yes)
    Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support 1.0 1.0 (1) 9.0 6.0 1.0

    Gecko notes

    • Prior to Firefox 6, the browser would throw if the useCapture parameter was not explicitly false. Prior to Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6), addEventListener() would throw an exception if the listener parameter was null; now the method returns without error, but without doing anything.

    Opera notes

    • Opera 12.00 made the useCapture parameter optional (source).

    WebKit notes

    • Although WebKit explicitly added "[optional]" to the useCapture parameter for Safari 5.1 and Chrome 13, it had been working before the change.

    See also

    EventTarget.addEventListener().

    Specification

    Polyfill to support older browsers

    addEventListener() and removeEventListener() are not present in older browsers. You can work around this by inserting the following code at the beginning of your scripts, allowing use of addEventListener() and removeEventListener() in implementations which do not natively support it. However, this method will not work on Internet Explorer 7 or earlier, since extending the Element.prototype was not supported until Internet Explorer 8.

    if (!Element.prototype.addEventListener) {
      var oListeners = {};
      function runListeners(oEvent) {
        if (!oEvent) { oEvent = window.event; }
        for (var iLstId = 0, iElId = 0, oEvtListeners = oListeners[oEvent.type]; iElId < oEvtListeners.aEls.length; iElId++) {
          if (oEvtListeners.aEls[iElId] === this) {
            for (iLstId; iLstId < oEvtListeners.aEvts[iElId].length; iLstId++) { oEvtListeners.aEvts[iElId][iLstId].call(this, oEvent); }
            break;
          }
        }
      }
      Element.prototype.addEventListener = function (sEventType, fListener /*, useCapture (will be ignored!) */) {
        if (oListeners.hasOwnProperty(sEventType)) {
          var oEvtListeners = oListeners[sEventType];
          for (var nElIdx = -1, iElId = 0; iElId < oEvtListeners.aEls.length; iElId++) {
            if (oEvtListeners.aEls[iElId] === this) { nElIdx = iElId; break; }
          }
          if (nElIdx === -1) {
            oEvtListeners.aEls.push(this);
            oEvtListeners.aEvts.push([fListener]);
            this["on" + sEventType] = runListeners;
          } else {
            var aElListeners = oEvtListeners.aEvts[nElIdx];
            if (this["on" + sEventType] !== runListeners) {
              aElListeners.splice(0);
              this["on" + sEventType] = runListeners;
            }
            for (var iLstId = 0; iLstId < aElListeners.length; iLstId++) {
              if (aElListeners[iLstId] === fListener) { return; }
            }     
            aElListeners.push(fListener);
          }
        } else {
          oListeners[sEventType] = { aEls: [this], aEvts: [ [fListener] ] };
          this["on" + sEventType] = runListeners;
        }
      };
      Element.prototype.removeEventListener = function (sEventType, fListener /*, useCapture (will be ignored!) */) {
        if (!oListeners.hasOwnProperty(sEventType)) { return; }
        var oEvtListeners = oListeners[sEventType];
        for (var nElIdx = -1, iElId = 0; iElId < oEvtListeners.aEls.length; iElId++) {
          if (oEvtListeners.aEls[iElId] === this) { nElIdx = iElId; break; }
        }
        if (nElIdx === -1) { return; }
        for (var iLstId = 0, aElListeners = oEvtListeners.aEvts[nElIdx]; iLstId < aElListeners.length; iLstId++) {
          if (aElListeners[iLstId] === fListener) { aElListeners.splice(iLstId, 1); }
        }
      };
    }
    

    Document Tags and Contributors

    Contributors to this page: Sheppy
    Last updated by: Sheppy,