onmousedown event causing body onload event to fire

Discussion in 'Javascript' started by Bender, Oct 22, 2003.

  1. Bender

    Bender Guest

    Hi,

    I am wanting to capture an onmousedown event without firing the body tags
    onload event.

    Also, if anyone could explain why this happens that would be excellent. I
    can't see how an onmousedown event could bubble up to an onload event.

    NOTE: If you put an alert() statement in the onmousedown event handler the
    body onload event doesn't get fired.

    Example code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <title>test page</title>
    </head>

    <body onload="alert('loading page');">
    <a href="./page.html" onmousedown="this.style.width = '25%';">test
    mouse down</a>
    </body>
    </html>
     
    Bender, Oct 22, 2003
    #1
    1. Advertising

  2. "Bender" <> writes:

    > I am wanting to capture an onmousedown event without firing the body tags
    > onload event.


    It never calls the body onload event. The onload event is triggered by
    the page being completely loaded. It only happens once, and doesn't
    wait for you to click.

    > Also, if anyone could explain why this happens that would be excellent. I
    > can't see how an onmousedown event could bubble up to an onload event.


    Let me guess: Your example code is saved as the file "page.html"?
    In that case, clicking on the a-element will execute the onmousedown event,
    and then follow the link and reload the page. That will ofcourse trigger
    the onload event on the new page.

    To stop the click from following the link, make the onmousedown event
    handler return false. That is, add "return false" at the end of the
    onmousedown attribute value.

    > NOTE: If you put an alert() statement in the onmousedown event handler the
    > body onload event doesn't get fired.


    That shouldn't change anything, except maybe make you move the mouse.
    Try using the keyboard to get rid of the alert (press return or
    escape), and see if it makes a difference.

    > <a href="./page.html" onmousedown="this.style.width = '25%';">test
    > mouse down</a>


    This test should fail. You try to set the width of an inline element,
    which should have no effect at all. The CSS width propert doesn't
    apply to inline elements. It will probably work in IE Quirks mode,
    but your DOCTYPE should put the browser into standards mode.

    Btw, your HTML is not legal. The a-element is an inline element, and
    cannot be directly inside the body tag. It needs a paragraph, or
    something similar, around it.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Oct 22, 2003
    #2
    1. Advertising

  3. Lasse Reichstein Nielsen wrote:

    > "Bender" <> writes:
    >> <a href="./page.html" onmousedown="this.style.width = '25%';">test
    >> mouse down</a>

    >
    > [...] The CSS width propert doesn't apply to inline elements.


    JFTR: The `width' property does not apply to _non-replaced_
    inline elements, table rows and row groups. Unfortunately,
    the `a' element is in fact a non-replaced inline element.

    > Btw, your HTML is not legal.


    Surely our robot won't be thrown in prison for that markup,
    although one should think about it ;-) The markup is not _valid_.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Oct 22, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QnJ5YW4=?=

    Onload Event Causing Error in ASP.NET Code

    =?Utf-8?B?QnJ5YW4=?=, May 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    865
    Teemu Keiski
    May 30, 2004
  2. Robert Oschler
    Replies:
    0
    Views:
    695
    Robert Oschler
    Jul 20, 2003
  3. Robert Oschler

    Body onload doesn't fire if frame load fails.

    Robert Oschler, Jul 20, 2003, in forum: Javascript
    Replies:
    1
    Views:
    225
    ManoDestra
    Jul 20, 2003
  4. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    614
    Martin Honnen
    Nov 4, 2004
  5. Replies:
    5
    Views:
    316
    Thomas 'PointedEars' Lahn
    May 15, 2005
Loading...

Share This Page