The Event object represents the state of the event, such as the element in which the event occurred, the state of the keyboard keys, the position of the mouse, and the state of the mouse buttons.
Events are often used in conjunction with functions, functions are not executed until the event occurs!
One: test button
function whichButton(event){ var btnNum = event.button; if (btnNum==2){
alert("You clicked the right mouse button!") } else if(btnNum==0) { alert("You clicked the left mouse button!") } else if(btnNum==1) { alert("You clicked the middle mouse button!"); } else { alert("You hit the " + btnNum+ " key, I can't determine its name."); } }
Two: get the mouse coordinates
function show_coords(event) { x=event.clientX;
y=event.clientY;
alert("X coordinate: " + x + ", Y coordinate: " + y) }
Three: Get the unicode of the keyboard keys
function whichButton(event) { alert(event.keyCode); }
Four: Mouse coordinates relative to the screen
function coordinates(event) { x=event.screenX;
y=event.screenY;
alert("X=" + x + " Y=" + y); }
Five: Whether the shift button is held down
function isKeyPressed(event) { if (event.shiftKey==1) { alert("The shift key was pressed!") } else { alert("The shift key was NOT pressed!") } }
Six: Test what element is clicked (p, div, or image, a and so on.)
function whichElement(e) { var targ if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3); // defeat Safari bug
targ = targ.parentNode;
var tname;
tname=targ.tagName;
alert("You clicked on a " + tname + " element."); }