The event object represents the state of the event that is associated with the object at a given time. This object contains a set of properties that hold additional information about the event that occurred. Although all event properties are available to all event objects, some properties don't have any meaning for various events. In other words, not all of the properties are relevant to each type of event.
We'll consider several properties of the event object. You can find the complete list for IE in other documentation.
if( event.type == "click" ){
do this
}
function mouse_move_handler()
{
document.all['move_obj_id'].style. posTop = event.clientY;
document.all['move_obj_id'].style. posLeft = event.clientX;
}
There is a complete example here.
| Code | Result |
|---|---|
function key_pressed()
{
document.inform.showcode.value += "Code: "+event.keyCode+
" char: "+String.fromCharCode(event.keyCode)+"\n";
}
...
<form name=inform>
<INPUT name=num TYPE=TEXT SIZE=25 onKeyPress="key_pressed();">
<br><br>
<textarea name=showcode rows=5 cols=40 onKeyPress="return false;"></textarea>
</form>
|
event.cancelBubble = true;This property can be very useful if both a parent element and a child element have event handlers for the same event, but you do not want the parent to handle the events occurred on the child element.
| Code | Result |
|---|---|
<form> <input type=text onKeyPress="if( event.keyCode<48 || event.keyCode>57 ) event.returnValue=false;"> </form> |
These are several links to documents discussing JavaScript events and event object:
Any String object has the only property length which contains the number of characters in the string. For example,
var str = "my string"; var i = str.length; // now i equals 9 var j = "wow".length; // now j equals 3
Besides the property length object String has a big set of methods that return a value based on the string. We've used method fromCharCode() which takes lists of codes and returns a string containing characters with the given codes:
var str = String.fromCharCode(109, 121, 32, 115, 116, 114, 105, 110, 103);
string_object_name.method_name()if we do not have any object to use we can use the name of the class:
String.method_name()but we cannot use these methods as usual functions.
The complete list of these methods can be found in appendix B.
| Code | Result |
|---|---|
<form>
<input type=text id=uinp size=5>
<input type=button value=" + 4 = "
onClick="document.all['res'].value = document.all['uinp'].value+4;">
<input type=text id=res size=5>
|
Function parseInt() takes two arguments. The first one is the string to convert and the second one is a number which value indicates the notation to use. The second argument is optional. If it's omitted, then the decimal notation is used. To use the function we need to do something like this:
var count = parseInt("123");
var hex_num = parseInt("AF3A", 16);
As you could see from the example when parseInt() is not able to convert a string it returns
NaN. Sometimes it seems that parseInt() return strange values. For example,
if we try to convert "103" using binary more (base=2) we'll get 2 as an answer. What happens is since "3"
is not an allowed digit in binary notation (only 0 and 1 are), then parseInt() converts only the
beginning of the string, that is, "10" which is exactly the binary notation for 2.
Experiments show that as a base we can use any decimal number between 2 and 36 (including both ends).
Function parseFloat() works very similar. There are only two differences with parseInt():
var avg = parseFloat("123.34");
Try to use it by your own:
Finally we would like to mention one more function of the top level (not associated with any object) isNan(). This function takes a number as an argument and returns true if it's NaN, otherwise it returns false. It's very useful to check the result of the conversion. For example:
var str = "af34.34";
var x = parseFloat(str);
if( isNaN(x) )
alert("Error: '"+str+"' is not a floating point number");
else
alert(x+" is a valid number");
To perform the inverse operation, that is, to convert a number into a corresponding string we can use method toString() of the object Number. Actually, just like strings, any numeric variable is in fact an instance of the Number object. This object has method toString() to convert number into a string. We can use it like the example shows:
var x = 15; var str = x.toString(); var str2 = x.toString(2);This method takes one argument which indicates what notation to use. For example, if we want to convert the value of the variable x to the hex notation we can do this:
str= x.toString(16);Here is a small example which converts a number entered by user into a float and then writes it back using the specified notation:
The following example combines function parseInt() and method toString() to show how to create a small converter between the most popular notations (dec/bin/hex):
| Code | Result |
|---|---|
function convert_from_dec()
{
var num = parseInt(document.converter.dec.value);
document.converter.dec.value = num;
document.converter.bin.value = num.toString(2);
document.converter.hex.value = num.toString(16);
}
function convert_from_hex()
{
var num = parseInt(document.converter.hex.value, 16);
document.converter.dec.value = num.toString();
document.converter.bin.value = num.toString(2);
document.converter.hex.value = num.toString(16);
}
function convert_from_bin()
{
var num = parseInt(document.converter.bin.value, 2);
document.converter.dec.value = num.toString();
document.converter.bin.value = num.toString(2);
document.converter.hex.value = num.toString(16);
}
...
<form name=converter>
<table>
<tr>
<td>Dec:</td> <td><input type=text name=dec> </td>
<td><input type=button value='Convert' onclick="convert_from_dec();"> </td></tr>
<tr>
<td> Hex:</td> <td> <input type=text name=hex> </td>
<td><input type=button value='Convert' onclick="convert_from_hex();"> </td></tr>
<tr>
<td> Bin:</td> <td> <input type=text name=bin> </td>
<td><input type=button value='Convert' onclick="convert_from_bin();"> </td></tr>
</table>
</form>
|