More JavaScript
Apr 14, 2024
Browser support
JavaScript works on almost all browsers
Internet Explorer uses JScript (referred to in menus as
“Active Scripting”), which is Microsoft’s dialect of
JavaScript
Older browsers don’t support some of the newer
features of JavaScript
We will assume modern browser support
Enabling and disabling JavaScript:
See http://www.valleyvet.com/si_javascript_help.html
2
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
To turn off active scripting in Outlook Express, see
http://support.microsoft.com/support/kb/articles/Q192/8/46.ASP
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)
3
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
4
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
5
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 NUL \v vertical tab
\b backspace \' single quote
\f form feed \" double quote
\n newline \\ backslash
\r carriage return \xDD Unicode hex DD
\t horizontal tab \xDDDD Unicode hex DDDD
6
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
7
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. 8
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
9
Interesting, right?
This is just a sneak preview of the full presentation. We hope you like
it! To see the rest of it, just
click here to view it in full on PowerShow.com. Then, if you’d like, you
can also log in to PowerShow.com to download the entire
presentation for free.