SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Canceling event bubbling and default action in Internet Explorer

    Hi all!

    Please look at the following code:

    if (window.event) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
    return false;
    }

    Question: Is the last statement (return false) really needed in canceling event bubbling and default action in IE??

    Thanks in advance for any help

    Kurt
    Last edited by Kurt; Jul 23, 2006 at 10:42.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Location
    Leeds, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think it's needed, here's the one I use if it helps you mate;

    Code:
    /* Stop events being fired in parents (will stop links opening after an onClick)
    ----------------------------------------------------------------------- */
    function noBubble(e) {
    if(window.event){window.event.cancelBubble=true;window.event.returnValue=false}if(e&&e.stopPropagation&&e.preventDefault){e.stopPropagation();e.preventDefault()}
    }

  3. #3
    SitePoint Member
    Join Date
    Oct 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello NakedIntruder,

    thanks for your prompt help!

    Kurt

    ps. the code is from sitepoints-book "dhtml javascript and dom" by langridge. my personal experience was that I dont need the last line, but its good to have the confirmation of an javascript-expert (I am a hobby-developer only)

  4. #4
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    window.event.returnValue = false; - dom method of canceling bubble

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Location
    Leeds, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kurt
    ps. the code is from sitepoints-book "dhtml javascript and dom" by langridge. my personal experience was that I dont need the last line, but its good to have the confirmation of an javascript-expert (I am a hobby-developer only)
    I got it from there too hehe, I'm no expert though so don't rely on what I'm currently using!

  6. #6
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cancelBubble and returnValue, both IE-only methods of the event object, are different in function. cancelBubble prevents an event from propagating up the container chain of element objects. This means that the event still registers on the target object, say a span contained within a div, within the body, etc. If cancelBubble is invoked, the event doesn't bubble up past the span. If cancelBubble is not invoked, the event is passed up the chain, step by step, until the document or window container, depending on the particular browser. Setting returnValue = false; however, means that even the target object essentially ignores the event.

    The DOM methods that correspond are stopPropagation and preventDefault.

    Note: Edited per Kurt's accurate observation, below.
    Last edited by JVLB; Jul 22, 2006 at 22:09.

  7. #7
    SitePoint Member
    Join Date
    Oct 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JVLB
    cancelBubble and returnValue, both IE-only methods of the event object, are different in function. cancelBubble prevents an event from propagating up the container chain of element objects. This means that the event still registers on the target object, say a span contained within a div, within the body, etc. If cancelBubble is invoked, the event doesn't bubble up past the span. If cancelBubble is invoked, the event is passed up the chain, step by step, until the document or window container, depending on the particular browser. Setting returnValue = false; however, means that even the target object essentially ignores the event.
    JVLB,

    Thanks for explaining the difference between cancleBubble and returnValue

    Regards

    Kurt

    BTW: When I understood you right , there is a small typo in the statement and it should say:

    If cancelBubble is not invoked, the event is passed up the chain, step by step, until the document or window container, depending on the particular browser.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •