Online JavaScript Tutorials

JavaScript tutorials which will help you use javaScripts on your web pages, even beginners. This javascript tutorial also includes links to other programming sites.

Get key press events using JavaScript

Get Key Press Events Using JavaScript
script language="javascript" type="text/javascript"

function CheckPressedKey(Key)

{
switch(KeyID)
{
case 13:
alert("Key Pressed: Enter");
break;

case 16:
alert("Key Pressed: Shift");
break;

case 17:
alert("Key Pressed: Ctrl");
break;

case 18:
alert("Key Pressed: Alt");
break;

case 19:
alert("Key Pressed: Pause");
break;

case 37:
alert("Key Pressed: Arrow Left");
break;

case 38:
alert("Key Pressed: Arrow Up");
break;

case 39:
alert("Key Pressed: Arrow Right");
break;

case 40:
alert("Key Pressed: Arrow Down");
break;
}

}


End script

CheckPressedKey(event)

JavaScript trim function Equivalent of PHP trim Function

This function returns a string with whitespace stripped/removed from the beginning and end of string . Without the second parameter, trim() will strip these characters:

* " " (ASCII 32 (0x20)), an ordinary space.
* "\t" (ASCII 9 (0x09)), a tab.
* "\n" (ASCII 10 (0x0A)), a new line (line feed).
* "\r" (ASCII 13 (0x0D)), a carriage return.
* "\0" (ASCII 0 (0x00)), the NUL-byte.
* "\x0B" (ASCII 11 (0x0B)), a vertical tab.

In PHP we can easily use trim($string) and all whitespace are stripped And in JavaScript we use this function to stripped the White spaces.

function trim(sValue)
{
return sValue.replace(/^\s+|\s+$/g, "");
}