Is there a bug in this code example from Beginning JavaScript with DOM Scripting and Ajax or I am missing something?

I'm reading a book called Beginning JavaScript with DOM Scripting and Ajax (2006)

On Page 168/139 it offers some example code, a stopBubble function, which promises to prevent an event from propagating in both IE and DOM-2 Compliant browsers:

stopBubble:function(e){
  if(window.event && window.event.cancelBubble){
    window.event.cancelBubble = true;
  }
  if (e && e.stopPropagation){
    e.stopPropagation();
  }
}

Since the cancelBubble property is a boolean property that defaults to false, I am wondering how we will ever reach the line where cancelBubble is set to true without it aleady being true. In other words that condition seems pointless.

I would not call myself an expert on JavaScript and so am wondering if there is something that I am missing (perhaps a hack utilizing some aspect of JavaScript's unintuitive truthiness). There is no errata logged for this in the book's Errata page. I suspect this is a mistake - but not 100% sure. Is it a broken example?

Answers


Seems you are correct and should notify the author


Need Your Help

css + first load bug + min-height

css

We forked an editor for upcoming release of our CMS.

How to get value of application variable in JavaScript from asp.net?

javascript asp.net

I want to access the value of an application variable in JavaScript. How can I do that?