4

My mouse has two buttons on the side, whose default behaviour are "Back" and "Forward".

What I'd like to know is whether or not it's possible to detect clicks of these mouse buttons in JavaScript, or if these are "special" buttons akin to the keyboard's "Play", "Volume Up" and "Wireless on/off" buttons.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • You can try whether the events get fired: http://jsfiddle.net/pimvdb/2c8gs/2/. Could you focus the result iframe and press those mouse buttons? – pimvdb Oct 26 '11 at 17:43

1 Answers1

3

I am not aware of any specific mouse events.

You can, however, easily find out yourself by inspecting the event object of the mousedown event. Fullscreen fiddle: http://jsfiddle.net/dWfDL/1/show/

var text = typeof document.body.textContent != "undefined" ? "textContent" : "innerText";
window.onmousedown = function(e){
    //Inspect the `e` object, for example using a for(var i in e) loop, or:
    //console.log(e);
    var s = [];
    for(var i in e){
        try{
            if(e[i] !== null){
                if(i.toUpperCase() == i) continue; //Ignore constants
                if(typeof e[i] == "function") continue; //Ignore functions
                if(e[i].parentNode) continue; //Ignore elements
                if(e[i] === window) continue; //Ignore Window
            }
            s.push(i + " =\t\t" + e[i]);
        }catch(err){s.push(i + " \tERROR reading property.")}
    }
    e.preventDefault();
    s = s.join("\n") + "\n\n";
    document.body[text] = s + document.body[text];
}
//Double-click to wipe contents
window.ondblclick = function(){
    document.body[text] = "";
}
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 1
    You may want to add `return false` or `e.preventDefault()` since the mouse seems to tell the browser to change pages. And if the browser doesn't intercept, then you know it's not available. – Dominic Barnes Oct 26 '11 at 17:47
  • @DominicBarnes Good point. I will include it at my next answer update (I'm creating a fiddle for this purpose). – Rob W Oct 26 '11 at 17:52
  • Okay, so the answer to my question is "no", but this helped me realise that `event.which` is supported in all major browsers (except IE 8 and lower), which let me fix up something elsewhere in my code. Thanks! – Niet the Dark Absol Oct 26 '11 at 18:26