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,309
  • 11
  • 42
  • 82

4 Answers4

3

You need to pass the event object to your function:

onmouseup="cclick(event);"
nnnnnn
  • 143,356
  • 28
  • 190
  • 232
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,631
  • 9
  • 68
  • 84
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
  • 6,961
  • 7
  • 43
  • 69
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,433
  • 5
  • 40
  • 62
  • 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