More JavaScript Browser support JavaScript works on the following browsers Netscape 2 and up Opera 3 and up Hotjava 3 and up iCab on Mac Classic Omniweb and WebTV on Mac OS X QNX Voyager and Konquerer on Linux KDE Internet Explorer uses JScript which is Microsoft s version of JavaScript Older browsers don t support some of the newer features of JavaScript We will assume modern browser support What you can t do To protect the visitor to your web pages you can t Read or write user files However JScript on IE allows ASP scripting which is how the very destructive JS Gigger A mm worm spreads Execute any other programs Connect to any other computer except to download another HTML page or to send e mail Determine what other sites the user has visited Open a very small less than 100px by 100px window or an offscreen window except in IE Debugging Mozilla Netscape has much better debugging tools than IE Mozilla Select Tools Web Development JavaScript console Netscape 6 Select Tasks Tools JavaScript console Netscape 4 Select Communicator Tools JavaScript console Any Mozilla or Netscape Type javascript in the location bar and press Enter Internet Explorer Go to the Preferences dialog and look for something like Web content Show scripting error alerts After debugging test your program in IE IE is the most popular browser Numbers In JavaScript all numbers are floating point Special predefined numbers Infinity Number POSITIVE INFINITY the result of dividing a positive number by zero Number NEGATIVE INFINITY the result of dividing a negative number by zero NaN Number NaN Not a Number the result of dividing 0 0 NaN is unequal to everything even itself There is a global isNaN function Number MAX VALUE the largest representable number Number MIN VALUE the smallest closest to zero representable number Strings and characters In JavaScript string is a primitive type Strings are surrounded by either single quotes or double quotes There is no character type Special characters are 0 b f n r t NUL backspace form feed newline carriage return horizontal tab v vertical tab single quote double quote backslash xDD Unicode hex DD xDDDD Unicode hex DDDD Some string methods charAt n Returns the nth character of a string concat string1 stringN Concatenates the string arguments to the recipient string indexOf substring Returns the position of the first character of substring in the recipient string or 1 if not found indexOf substring start Returns the position of the first character of substring in the given string that begins at or after position start or 1 if not found lastIndexOf substring lastIndexOf substring start Like indexOf but searching starts from the end of the recipient string More string methods match regexp Returns an array containing the results or null if no match is found On a successful match If g global is set the array contains the matched substrings If g is not set Array location 0 contains the matched text Locations 1 contain text matched by parenthesized groups The array index property gives the first matched position replace regexp replacement Returns a new string that has the matched substring replaced with the replacement search regexp Returns the position of the first matched substring in the given string or 1 if not found boolean The boolean values are true and false When converted to a boolean the following values are also false 0 0 and 0 the empty string or undefined null NaN undefined and null There are special values undefined and null undefined is the only value of its type This is the value of a variable that has been declared but not defined or an object property that does not exist void is an operator that applied to any value returns the value undefined null is an object with no properties null and undefined are but not Arrays As in C and Java there are no true multidimensional arrays However an array can contain arrays The syntax for array reference is as in C and Java Example var var var var a red b a 1 0 c a 1 d c 1 255 green 128 b is now green c is now green 128 d is now 128 Determining types The unary operator typeof returns one of the following strings number string boolean object undefined and function typeof null is object If myArray is an array typeof myArray is object To distinguish between different types of objects myObject instanceof Constructor The Constructor should be an object that is a constructor function It is an error if the rhs is not an object at all myObject constructor Constructor myObject toString ConstructorName Wrappers and conversions JavaScript has wrapper objects for when a primitive value must be treated as an object var s new String Hello s is now a String var n new Number 5 n is now a Number var b new Boolean true b is now a Boolean Because JavaScript does automatic conversions as needed wrapper objects are hardly ever needed JavaScript has no casts but conversions can be forced var s x s is now a string var n x 0 n is now a number var b x b is now a boolean Because JavaScript does automatic conversions as needed explicit conversions are hardly ever needed Variables Every variable is a property of an object When JavaScript starts it creates a global object In client side JavaScript the window is the global object It can be referred to as window or as this The built in variables and methods are defined here There can be more than one global object For example one frame can refer to another frame with code such as parent frames 1 Local variables in a function are properties of a special call object HTML names in JavaScript In HTML the window is the global object It is assumed that all variables are properties of this object or of some object decended from this object The most important window property is document HTML form elements can be referred to by document forms formNumber elements elementNumb er Every HTML form element has a name attribute The name can be used in place of the array reference Hence if form name myForm input type button name myButton Then instead of document forms 0 elements 0 you can say document myForm myButton More about with with object statement uses the object as the default prefix for variables in the statement As noted in an earlier lecture one book hints at mysterious problems resulting from the use of with and recommends against ever using it It turns out that there are two problems with is difficult to optimize hence may be inefficient More importantly variable declarations and function definitions have odd and
View Full Document