/ Published in: JavaScript
Deals with the fact that mouse buttons are referenced in different ways by different browsers.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Browser detection var ie=document.all != null; //ie4 var op7=navigator.userAgent.indexOf("opera")>0 && operaVersion() <= 7; function operaVersion() { agent = navigator.userAgent; idx = agent.indexOf("opera"); if (idx>-1) { return parseInt(agent.subString(idx+6,idx+7)); } } /* Detection of the mouse button L M R IE,KON 1 4 2 event.button NS,OP8,FF 0 1 2 e.button OP7 1 3 2 e.button NS,OP8,FF 1 2 3 e.which */ var leftButton = ie? 1 : 0; // op7 supports document.all var middleButton = op7 ? 3 : ie ? 4 : 1; var rightButton = 2; document.onmouseup = onClick; // This code is executed each time a mouse button is released function onClick(e) { if (ie) { var elem = event.srcElement; var btn = event.button; //e = event; } else { var elem = e.target; var btn = e.button; } // elem is the element the user clicked on // btn is the mouse button which was used // e.g. if (btn == leftButton) { alert( elem + ": You clicked me!" ); } /* ...your code goes here... */ return false; }