onmousedown event causing body onload event to fire

B

Bender

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>
 
L

Lasse Reichstein Nielsen

Bender said:
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
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Bender said:
<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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top