JavaScript Reference
trim()
Syntax:
string .trim()
This method returns a copy of string withou t any leading or trailin g w hitespace.
trim() do es not ch ange string .
Example:
var s = " Too much whitespace. ";
s.trim() //Returns "Too much whitespace."
valueOf()
Syntax:
string .valueOf()
Same as toString().
E.17 window (Client-Side JavaScript)
The window global property is a reference to the Window object, which r epresents
a browser window or tab. It ac ts as a global object in client-side JavaScript, which
means that you do not have to refe r to it explicitly. All properties and metho ds of the
Window object can b e used as though th ey were global variables and functions. For
example, if you want to invoke the setTimeout( ) method of Window, you simply
use se tTimeout() instead of window.setTim eout(). Also, all global variables and
functions that you define in your script are in fact properties and methods o f Window.
Some of the properties and methods of Window actua lly operate on the browser
window. Others are here merely as generic global variables and functions. Apart
from these, Win dow also implements all the global variables a nd functions of core
JavaScript. Some o f them are listed in section E.11 on page 406 of this appendix .
A peculiar thing is that window, which refers to the Window object, is the property
of the Window object itself. This makes the expressions window, window.window,
window.window.window, and so on all return the same Window object.
E.17. window (Cl ient-Side JavaScript) 42 7
Properties
document
Syntax:
document
window.document
A reference to the Document object that holds the content of window. For a detailed
description of the Document ob je ct, re fer to section E.7 on page 391.
innerHeight, innerWidth
Syntax:
innerHeight
innerWidth
window.innerHeight
window.innerWidth
These properties specify the pixel dimensions of the document display area of window.
localStorage
Syntax:
localStorage
window.localStorage
A reference to a Storage object that implements client- sid e data storage. The Storage
object p rovides means of storing nam e/value pairs on a local computer and recalling
them any time in the future.
location
Syntax:
location
window.location
The location proper ty holds the URL of the document that is cu rrently displayed
within this window. If you set location to some other URL, the browser will load
and display the document from tha t new URL.
428 JavaScript Reference
JavaScript Reference
window
Syntax:
window
The window property holds a refer ence to the Window object representing the cur-
rent browser window. It is rarely necessary to use this property explicitly because
the Window object is the glob al object of client-side JavaScript. This makes all the
properties and methods of Window accessible without explicitly refe rring to window.
However, the window property is often used explicitly wh en specifying the onload
event handler on Window.
Methods
alert()
Syntax:
window.alert(message )
Displays message in plain text inside an alert dia log box. The dialog h as a single OK
button and is modal, which means that a call to alert() blocks the code execution
until the user closes the dialog.
clearInterval()
Syntax:
window.clearInterval(intervalID )
This method cancels the repeating of the code execution th at was scheduled by a
previous call to setInterval(). The intervalID argument should have the same
value as returned by a corr esponding call to setInterval().
Example:
h1 = setInterval(f1, 100);
h2 = setInterval(f1, 5000);
//Stops the repeating of the execution of f1:
clearInterval(h1);
E.17. window (Cl ient-Side JavaScript) 42 9
clearTimeout()
Syntax:
window.clearTimeout(timeoutID )
This meth od cancels the pending code execution that was scheduled by a previous call
to setTimeout(). T he timeoutID argument shou ld have the same value as returne d
by a corresponding call to setTimeout().
confirm()
Syntax:
window.confirm(question )
Displays question in plain text inside a confirm dialog box. The dialog has OK
and Cancel buttons for the user to answer the question. If the user selects OK, then
confirm() returns true, otherwise it returns false. The confirm dialog is modal,
which means that the call to confirm() blocks the c ode execution until the dialo g
closes after clicking
OK or Cancel.
prompt()
Syntax:
window.prompt(message )
window.prompt(message , defaultText )
This method opens a prompt dialog with the in dicated plain text message , a text
input field, and
OK and Cancel buttons. Dialog waits until the user types some text
into the text input field and clicks either
OK or Cancel. If the user clicks the OK
button, then prompt() returns the string o f text that is cu rrently shown in the input
field. Otherwise, if the user clicks
Cancel, prompt() returns nu ll.
The optional defaultText argument g ives the initial value of the text inpu t field.
In modern web programmin g, this method is seldo m used and ma ny conside r it ba d
design. That said, it can be quite c onvenient for a beginner to capture the user input.
setInterval()
Syntax:
window.setInterval(function , interval , arg1 , arg2 , ..., argN )
This method schedules function to be invoked at every interval milliseconds.
Zero or more optional arguments (arg1 , arg2 , ..., argN ) will be passed to function .
430 JavaScript Reference
JavaScript Reference
Note that function is invoked as a method of the Window object, which means
that the th is keyword refers to the Window object when used inside the body of
function .
The setInterval() method returns a value that can later be used as an argum ent of
clearInterval() to cancel the repetition of the invocation of function .
setTimeout()
Syntax:
window.setTimeout(function , timeout , arg1, arg2 , ..., argN )
This method is like set Interval(), except that it schedules a function invocation
only once: function will be invoked after timeout m illiseconds.
The setTimeout() me thod returns a value that can later b e used as an argume nt of
clearTimeout() to cancel the pending invocation of function .
Events
You can also use all of the event-handler properties that are defined on Element on the
Window objec t. T hat is possible because most events bubble up the document tree all
the way to the Window object. Window also defines some event-hand le r properties of
its own (e.g. , the onstorage event-handler property) but they are out of the scope o f
this b ook.
onload
Syntax:
window.onload = handlerFunction ;
This is a n event handler for the load event on the Window object, which is on e of the
most important handlers. The load event fires after the document, and all the external
resources referred to from within the document have been fully loaded and are ready
to be manipulated. The specified handlerFunction function is where JavaScript
code normally begins its execution.
Take, for example , the following cod e:
window.onload = function() {
document.getElementById("status").innerHTML =
"Boarding completed.";
};
Suppose there is an HT ML element with ID of status in the c orresponding docu-
ment. After the documen t has loaded, you see the text “Boarding completed. written
in it.
E.17. window (Cl ient-Side JavaScript) 43 1
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.255.44