June 11, 2012

stopPropagation vs. cancelBubble

There are many occasions when you have to override the default action of an event. The browser has to go through every single ancestor element of the event target to see if it has an event handler.

For this reason we very often must stop the event propagation. Here is my function that I use to stop event propagation in every kind of web browser.
<script type="text/javascript">
  function StopEventPropagation(oEvent) {  
    if(!oEvent) oEvent = window.event;
    if(oEvent) {
      if(typeof(oEvent.cancelBubble)=="boolean") {
        oEvent.cancelBubble = true;
      }
      else if(oEvent.stopPropagation) {
        oEvent.stopPropagation();
      }
    }
  }
</script>

No comments:

Post a Comment