-2

I need to change the action of righ click and middle click to left click, so that when someone right clicks or middle clicks (mouse wheel click) inside a page, it will always act as a left click.

How is that possible?

andrewsi
  • 10,807
  • 132
  • 35
  • 51
Ehsan ...
  • 9
  • 3
  • 2
    And why would you want to so screw with your users' expectations? – David Thomas Sep 15 '12 at 15:28
  • You can use jQuery - http://stackoverflow.com/questions/1795734/triggering-onclick-event-using-middle-click – mjastrzebowski Sep 15 '12 at 15:31
  • Please don't override the default without good reasons. – Markus Unterwaditzer Sep 15 '12 at 15:54
  • @Markus then should programmers not be able to make the page do something when the user simply clicks a button? To me, thats overriding, so whats the problem? – Ian Sep 16 '12 at 02:31
  • @ianpgall He doesn't want to add extended behavior, he just wants to disable rightclick and make it do the same thing as leftclick. – Markus Unterwaditzer Sep 16 '12 at 07:35
  • @DavidThomas My php website is compleatly inside an iFrame. (an html that contains a php) when users right click and open new window or middle click, the inside page opens in a new window without the border. I thought I could disable middle click and right click to prevent that. do you have a better idea? – Ehsan ... Sep 16 '12 at 14:12
  • I have managed to disable the right click easily with: oncontextmenu="return false" Can you tell me something like this to also prevent the middle click? – Ehsan ... Sep 16 '12 at 14:13
  • @MarkusUnterwaditzer I understand that, they're extending the behavior, so what's the big deal? Even if they wanted to prevent a left or right click, then deal with it. If the user wants to visit the website, they are under control of the website, so if everything is backwards, that's up to the programmer and it's not up to you to decide if it's "correct". If users don't like it, the owner will hear it, otherwise it's no one's concern. My point was that if you change anything about normal behavior, that's technically overriding, and you're arguing against even the simplest changes – Ian Sep 16 '12 at 15:39
  • @ianpgall Yes, my statement that this is wrong was an opinion. That's why i posted it as a comment, and not as an answer. – Markus Unterwaditzer Sep 16 '12 at 19:21
  • @DavidThomas Actually this is a perfectly reasonable question. There are times when right- and middle-clicks make no sense, like inside a drawing canvas, and treating them as a left-click makes as much sense as anything else. – Jez Sep 05 '16 at 16:43

2 Answers2

2

require jquery

 $(document).live('click', function(e) {
       if( e.which != 1 )
       {
        var target = e.target;
            target.click();
         e.preventDefault();
       }
    });

this is my basic test on Fiddle hope it works for you

http://jsfiddle.net/GSXDJ/15/

Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
1

You can detect what button was clicked using the event.which property, f.ex:

​document.onclick = function(e) {
    alert(e.which);
};​

http://jsfiddle.net/R3LVW/

1 is left, 2 and 3 is middle and right. Now add your own logic to that. If you need to normalize between browsers, I recommend jQuery for events.

David Hellsing
  • 106,495
  • 44
  • 176
  • 212
  • (without jQuery) you would need more checking because just using `event.which` isn't completely cross-browser compatible. Just saying – Ian Sep 15 '12 at 15:52
  • @ianpgall correct, that is why I recommended jQuery if the OP needs cross-browser support. – David Hellsing Sep 15 '12 at 21:35
  • @David Thank you for your answer. Can you also tell me what should I write instead of alert(e.which); to trigger the normal click? (left click) – Ehsan ... Sep 16 '12 at 14:17
  • @Ehsan... that depends, but you can try (using jQuery): `e.preventDefault(); $(this).click();` – David Hellsing Sep 16 '12 at 15:16
  • @David Sorry, I guess I didn't see your jQuery stipulation – Ian Sep 16 '12 at 15:40
  • @David Thanks. I will try it. In another try I have managed to disable the right click easily with: oncontextmenu="return false" Can you tell me something like this to also prevent the middle click? – Ehsan ... Sep 16 '12 at 16:30