2

I have a button, and I decided to trigger that button:

$(button_name).trigger("click");

The handler of the function requires that the left button triggers the click when triggering using the mouse, but also does accepts clicks by triggering using code, like the line shown above. How can I create a conditional statement which evaluates if such conditions are true?

if((navigator.appName === "Microsoft Internet Explorer" && window.event.button === 1) 
    || window.event.button == 0
    || ...){

    /* If true, do this */
}
slugster
  • 49,403
  • 14
  • 95
  • 145
Jesufer Vn
  • 13,200
  • 6
  • 20
  • 26
  • I think any way you can find to distinguish between the two, can be patched (emulated). – bart Jul 17 '11 at 09:03

2 Answers2

4

The first argument to the click handler is the event object. If it's a physical mouse click it will have a clientX and clientY property- if it's programmatic, it won't.

$('#button').click(function (e) {
    if (e.clientX) {
        // physical click
    } else {
        // program click
    } 
});
evan
  • 4,239
  • 1
  • 18
  • 18
  • [`originalEvent`](http://stackoverflow.com/questions/6674669/in-jquery-how-can-i-tell-between-a-programatic-and-user-click/6674806#6674806) is a better choice and what happens if `clientX` is zero? – mu is too short Jul 17 '11 at 05:57
0

If you're trying to have complete cross-browser support and future proof it, I don't think you can, js is remarkably compliant when it comes to adding and modifying properties, but my question is "should you?" There have been a couple of times where the ability to simulate a Mouse click has actually saved a good deal of time and energy.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • 1
    I'd agree that it shouldn't matter but checking the [`originalEvent`](http://stackoverflow.com/questions/6674669/in-jquery-how-can-i-tell-between-a-programatic-and-user-click/6674806#6674806) property of the jQuery event should be pretty bullet proof. If jQuery didn't wrap and normalize all the events then you'd probably be out of luck unless an approximate solution was good enough. – mu is too short Jul 17 '11 at 06:00
  • @mu The problem is that unless you are really willing to put in hours and hours of work into trying to bust this type of stuff, then will it really be effective? I don't see the effort being worth the cost in this case. – cwallenpoole Jul 17 '11 at 06:09
  • We don't have enough details to know if all this chicanery is worthwhile in this case. But you don't need to bother with cross-browser issues because jQuery wraps the event so you only have to bother with what jQuery is doing. In any case, worrying about it probably pointless. – mu is too short Jul 17 '11 at 06:22