/ Published in: JavaScript
Source: eventinfo @ howtocreate
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* Input, textarea, select -> onclick, onkeydown, onkeyup, onkeypress, onchange, onfocus, onblur, etc. form -> onsubmit, onreset a -> onclick, onmouseover, onmouseout, onfocus, onblur body -> onload, onunload */ // ------------------------------ // reacting to an event <a onmouseover="name_of_function(params)" href="somepage.html"> <input onkeypress="myVariable = 2;startTest();"> <select onchange="self.location.href = this.options[this.selectedIndex].value;"> <body onunload="window.alert('bye')"> // ------------------------------ // Executing a function <a href="javascript:name_of_function(params)"> // ------------------------------ // Accommodating Netscape 4 //Only Netscape 4 and Escape 4 need this first line if( document.captureEvents && Event.KEYUP ) { document.captureEvents( Event.KEYUP ); } document.onkeyup = alertkey; //where alertKey is a function that will handle the event // ------------------------------ // Detecting the keyup event over the page and extracting // the key code. /* -> iCab only passes key code information in input boxes. -> Blazer (a version of NetFront) detects limited key events (due to the keyboard handling on the device) and returns nonsense key codes (such as -1987304). -> WebTV and Escape 4 can only detect key events on text boxes, and may be a little unreliable. -> OmniWeb 4.2- and Opera 5 for Mac do not pass key code information. -> Netscape 4 on Linux does not detect any key events properly. -> Clue browser cannot detect key events. I am unsure of the capabilities of NetBox, iPanel MicroBrowser and OpenTV here. */ // first, tell the browsers to react to the event if( document.captureEvents && Event.KEYUP ) { //remove this part if you do not need Netscape 4 to work document.captureEvents( Event.KEYUP ); } /* this next line tells the browser to detect a keyup event over the whole document and when it detects it, it should run the event handler function 'alertkey' */ document.onkeyup = alertkey; //now create the event handler function to process the event function alertkey(e) { if( !e ) { //if the browser did not pass the event information to the //function, we will have to obtain it from the event register if( window.event ) { //Internet Explorer e = window.event; } else { //total failure, we have no way of referencing the event return; } } if( typeof( e.keyCode ) == 'number' ) { //DOM e = e.keyCode; } else if( typeof( e.which ) == 'number' ) { //NS 4 compatible e = e.which; } else if( typeof( e.charCode ) == 'number' ) { //also NS 6+, Mozilla 0.9+ e = e.charCode; } else { //total failure, we have no way of obtaining the key code return; } window.alert('The key pressed has keycode ' + e + ' and is key ' + String.fromCharCode( e ) ); } // ------------------------------ // Detecting the mouse coordinates when it moves // over a positioned element </h3> if( myReference.captureEvents && Event.MOUSEMOVE ) { //remove this part if you do not need Netscape 4 to work myReference.captureEvents( Event.MOUSEMOVE ); } myReference.onmousemove = alertCoord; function alertCoord(e) { if( !e ) { if( window.event ) { //Internet Explorer e = window.event; } else { //total failure, we have no way of referencing the event return; } } if( typeof( e.pageX ) == 'number' ) { //most browsers var xcoord = e.pageX; var ycoord = e.pageY; } else if( typeof( e.clientX ) == 'number' ) { //Internet Explorer and older browsers //other browsers provide this, but follow the pageX/Y branch var xcoord = e.clientX; var ycoord = e.clientY; var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) || ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) || ( navigator.vendor == 'KDE' ) if( !badOldBrowser ) { if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { //IE 4, 5 & 6 (in non-standards compliant mode) xcoord += document.body.scrollLeft; ycoord += document.body.scrollTop; } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { //IE 6 (in standards compliant mode) xcoord += document.documentElement.scrollLeft; ycoord += document.documentElement.scrollTop; } } } else { //total failure, we have no way of obtaining the mouse coordinates return; } window.alert('Mouse coordinates are ('+xcoord+','+ycoord+')'); }
URL: http://www.howtocreate.co.uk/tutorials/javascript/eventinfo