Java Reference
In-Depth Information
In general, you should refer to global variables without using the
window
object (it's less
typing and your code is more portable between environments). An exception is if you need
to check whether a global variable has been defined. For example, the following code will
throw an exception if
x
has not been defined:
if (x) {
// do something
}
However, if the variable is accessed as a property of the
window
object, an exception will
not occur (although the block of code will still not be evaluated without
x
being defined):
if (window.x) {
// do something
}
Some functions that we've already met such as
parseInt()
and
isNaN()
are global
functions, which in a browser environment makes them methods of the
window
object:
window.parseInt(4.2);
<< 4
window.isNaN(4.2);
<< false
Like variables, it is customary to omit accessing them through the
window
object.
Dialogs
In
Chapter 1,
we introduced three functions that produced dialogs in the browsers:
alert()
,
confirm()
, and
prompt()
. These are not part of the ECMAScript stand-
ard, although all major browsers support them as methods of the
window
object.
The
window.alert()
method will stop the execution of the program and display a mes-
sage in a dialog box. The message is provided as an argument to the method and
un-
defined
is always returned:
