0

Trying to detect left click vs right click (without using jQuery!) and I have the following code

Javascript:

function cclick(e) {
   if (e.button == 1) {alert("hgbdygwea");}
   else {
      alert(e.button);
   }
}

HTML:

<a onmouseup="cclick()" ...> <img .../> </a> <!-- also tried cclick instead of cclick() -->

using internet explorer 9

Oztaco
  • 3,399
  • 11
  • 45
  • 84

4 Answers4

3

You need to pass the event object to your function:

onmouseup="cclick(event);"
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

Quirksmode has a good write up on the subject of "Which mouse button has been clicked?" and his code works over the top of yours.

Marcel
  • 27,922
  • 9
  • 70
  • 85
0

Following is another approach

function cclick() {
   var e = window.event;
   if (e.button == 1) {alert("hgbdygwea");}
   else {
      alert(e.button);
   }
}

And call the function as below without passing the event.

<a onmouseup="cclick()" ...> <img .../> </a>
prageeth
  • 7,159
  • 7
  • 44
  • 72
0

I think this code should work for you. e.button is not valid for all browsers (and I cleaned up the code).

function cclick(e) {
    "use strict";
    e || window.event;
    if (e.which) {
        alert((e.which === 1) ? "hgbdygwea" : e.which);
    } else {
        alert((e.button === 1) ? "hgbdygwea" : e.button);
    }
}

<a onmouseup="cclick(event)" ...> <img .../> </a> <!-- also tried cclick instead of cclick() -->
Eric
  • 6,563
  • 5
  • 42
  • 66
  • which browsers is e.which for? i heard it was for netscape so i didnt bother with it since its not popular anymore? – Oztaco Apr 09 '12 at 08:01