2

i've developed a firefox addon with a simple button on the status bar. Now i want to get the right and the left click.

Mi code look like this below

<popupset>
    <menupopup id="intransContextMenu" position="overlap">
        <menuitem label="settings" onclick="alert('test');" type="checkbox"/>
        <menuitem label="test" onclick="alert('test nr 2');" type="checkbox"/>
    </menupopup>
</popupset>

<statusbar id="status-bar">
    <statusbarpanel id="2" label="test" tooltip="savetip" context="intransContextMenu"/>
</statusbar>

Right click on test label open the menu popup. It seems working, but the left click is triggered before the right click on the label TEST.

Is there any way that provide me isolating the right and the left click? Triggering left click when left click occours and right click when right click occurs.

Thanks

alesdario
  • 1,873
  • 6
  • 25
  • 38
  • You should avoid testing for left clicks on XUL elements, instead use `oncommand` as this captures keyboard access too. (You still need a click handler to test for other clicks.) – Neil Mar 30 '11 at 20:36

1 Answers1

5

You should create custom event handler, not just inline alert call, and inside that handler, you can recognize which button was pressed:

function whichButton(e)
{
    var btnCode = e.button;

    switch (btnCode){
      case 0  : alert('Left button clicked');
                break;
      case 1  : alert('Middle button clicked');
                break;
      case 2  : alert('Right button clicked');
                break;
      default : alert('Unexpected code: ' + btnCode);
    }
}

<popupset>
    <menupopup id="intransContextMenu" position="overlap">
        <menuitem label="settings" onclick="alert('test');" type="checkbox"/>
        <menuitem label="test" onclick="whichButton(event);" type="checkbox"/>
    </menupopup>
</popupset>

Also, to prevent appearing of context menu you can use oncontextmenu event handler:

<menuitem label="test" onclick="whichButton(event);" type="checkbox" oncontextmenu="event.preventDefault();"/>
Yuriy
  • 1,188
  • 1
  • 9
  • 7