Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capturing Mouse Clicks

Status
Not open for further replies.

mtorbin

Technical User
Nov 5, 2002
369
US
OK, what am I doing wrong here? I'm trying to have the left button / right button event captured:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
BLAH BLAH BLAH
<SCRIPT>
function doThis(e) {
   alert (e.button);
}
   document.captureEvents(Event.CLICK);
</SCRIPT>

<a href="#" onMouseDown="doThis(this);">Thingy</a>
</body>
</html>

Thanks,

- MT
 
OK, I got this much to work and I think I understand it thus far. Now I just need to capture what mouse button was clicked:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<SCRIPT>
function handleClickEvent(e) {
   alert (e.button);
}
document.captureEvents(Event.MOUSEDOWN);

</SCRIPT>
<a href="#" onMouseDown="handleClickEvent(this);">THINGY</a>
</body>
</html>
 
Mouse detection is different between Netscape and IE.
Netscape uses button while IE uses which and both of them use different return values for which button.

Try this code, it should work pretty well. I have not tested in anything other than IE though.
Also, notice that I added an extra parameter to the function. If you pass in a value from your onmousedown event you are overwriting the e value the browser would otherwise use and you get undefined.
If you do not need to pass a value then just eliminate it from the onmousedown event and the extra parameter from the function.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<SCRIPT>
function doThis(myval, e) {
  var whichbutton;
  if (!e) var e = window.event;
  if (e.which) whichbutton = (e.which == 1)?"Left":(e.which == 3)?"Right":"Middle";
  else if (e.button) whichbutton = (e.button == 1)?"Left":(e.button == 2)?"Right":"Middle";

  alert(whichbutton);
}

</SCRIPT>

<a href="#" onmousedown="doThis(this);">Thingy</a>
</body>
</html>

Stamp out, eliminate and abolish redundancy!
 
Hmmm... didn't work. I'm attempting to test it on a Mac. I wonder if that would be an issue?

- MT
 
I just noticed in the href you're only passing one value, but in the function you're requiring two.

- MT
 
Yes, the value for e seems to be pulled directly out of the browser. If you put no parameter in your event and only e in the function it works. If you want to use a parameter in the event then you need the extra parameter in the function or the value of e is getting replaced by the object you were passing from the onmousedown event.


Stamp out, eliminate and abolish redundancy!
 
This is the error that I got when I clicked on Thingy:

Error: e has no properties
Source File: testPage.html
Line: 13

- MT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top