Standard JavaScript objects.

Event

When any event happens a browser creates an event object. This object is available only during an event. Therefore, you can only refer to it in event handler scripts and event processing functions. The event object is governed by the window object, so its formal syntax is window.event. The preceding window specification is not necessary in any version of JavaScript, so it may be omitted.

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.

These are several links to documents discussing JavaScript events and event object:

  • Events And JavaScript
  • The Internet Explorer Event Model
  • The Navigator Event Model

    String

    As we mentioned before when a variable is assigned a string value it becomes string variable. Actually, this is not completely true. As a matter of fact, JavaScript does not support string types it creates a String object instead. Thus, any variable containing a string value or a constant string (like "Hello!") is an instance of the String 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);
    

    Please notice that since fromCharCode() is a method of the Sting object we need to use it as a method; that is, we have to write
    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.

    String conversion

    In this section we will discuss how convert strings into numbers and numbers into strings. We will need this type of conversion for calculator. Let us illustrate importance of this on example. Assume I need to add number 4 to the value user entered in an text input element. I can easily access this element by its ID or by its name and read its property value, which contains user's input:
    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>
    
    
       
    Try to work with this and make sure that "4+4" gives us "44". Very surprisingly! That happens because the value property of an input element is a string. So, the browser converts number 4 into a string as well and computes the sum of two strings, that is, just adds string with character 4 to the string entered by the user. Exactly the same thing happens if you type "a=" into the text element. If we need to compute the sum of these elements as numbers we first need to convert the user's input from String to a number. There are several top level functions, which can do the job. Words top level mean that those functions ore not members of any object and we can use them just calling their names (just like we use our own functions). These functions are:
  • parseInt() and
  • parseFloat()
    As you can guess they do the same job, but the second function can convert numbers that contain the decimal point.

    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);
    
    String:   Base:    
    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():

    1. this function doesn't stop when it sees a dot. It takes it as a decimal point. That is, it can covert strings containing floating point numbers into numbers.
    2. It takes only one argument -- string. It can not take the base argument because it works only with decimal notation.
    Here is a small example that show how to use this function:
    var avg = parseFloat("123.34");
    
    Try to use it by your own:
    String:    

    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:
    Number:   base:  

    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>
    
    Dec:
    Hex:
    Bin: