Return to Snippet

Revision: 271
at July 8, 2006 08:57 by MyKey_


Initial Code
// 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;
}

Initial URL


Initial Description
Deals with the fact that mouse buttons are referenced in different ways by different browsers.

Initial Title
Mouse Buttons on different Browsers

Initial Tags
event, browser, button

Initial Language
JavaScript