97
the specified name, a new window is created to display the contents of
url
. Otherwise, the
url
is loaded into the existing window with the specified name.
The
features
argument used to specify the window position, size, and features (such as
menubar, toolbar, etc.). In modern browsers that support tabs, it is often ignored, and it is
not documented here.
When you use
Window.open()
to load a new document into an existing window, the
replace
argument specifies whether the new document has its own entry in the window’s browsing
history or whether it replaces the history entry of the current document. If
replace
is
true
,
the new document replaces the old. If this argument is
false
or is not specified, the new
document has its own entry in the Window’s browsing history. This argument provides func-
tionality much like that of the
Location.replace()
method.
void postMessage(any message, string targetOrigin, [MessagePort[] ports])
Send a copy of the specified
message
and the optionally specified
ports
to this window, but
only if the document displayed in this window has the specified
targetOrigin
.
message
can be any object that can be cloned with the structured clone algorithm (see “Struc-
tured Clones” on page 672).
targetOrigin
should be an absolute URL that specifies the
scheme, host, and port of the desired origin. Or
targetOrigin
can be “*” if any origin is ac-
ceptable or “/” to use the script’s own origin.
Calling this method on a window causes a message event on that window. See
MessageEvent
and §22.3.
void print()
Calling
print()
causes the browser to behave as if the user had selected the browser’s Print
button or menu item. Usually, this brings up a dialog box that enables the user to cancel or
customize the print request.
string prompt(string message, [string default])
The
prompt()
method displays the specified
message
in a modal dialog box that also contains
a text input field and OK and Cancel buttons and blocks until the user clicks one of the
buttons.
If the user clicks the Cancel button,
prompt()
returns
null
. If the user clicks the OK button,
prompt()
returns the text currently displayed in the input field.
The
default
argument specifies the initial value of the text input field
void scroll(long x, long y)
This method is a synonym for
scrollTo()
.
void scrollBy(long x, long y)
scrollBy()
scrolls the document displayed in
window
by the relative amounts specified by
dx
and
dy
.
void scrollTo(long x, long y)
scrollTo()
scrolls the document displayed within
window
so the point in the document speci-
fied by the
x
and
y
coordinates is displayed in the upper left corner, if possible.
Window
1006 | Client-Side JavaScript Reference
118
long setInterval(function f, unsigned long interval, any args...)
setInterval()
registers the function
f
to be invoked after
interval
milliseconds and then to
be repeatedly invoked at that specified
interval
.
f
will be invoked with the Window as its
this
value, and will be passed any additional
args
that were passed to
setInterval()
.
setInterval()
returns a number that can later be passed to
Window.clearInterval()
to cancel
the execution of
code
.
For historical reasons,
f
may be a string of JavaScript code instead of a function. If so, the
string will be evaluated (as if it were a
<script>
) every
interval
milliseconds.
Use
setTimeout()
when you want to defer the execution of code but do not want it to be
repeatedly executed.
long setTimeout(function f, unsigned long timeout, any args...)
setTimeout()
is like
setInterval()
, except that it invokes the specified function only once: it
registers
f
to be invoked after
timeout
milliseconds have been elapsed and returns a number
that can later be passed to
clearTimeout()
to cancel the pending invocation. When the speci-
fied time has passed,
f
will be invoked as a method of the Window and will be passed any
specified
args
. If
f
is a string rather than a function, it will be executed after
timeout
millisec-
onds as if it were a
<script>
.
any showModalDialog(string url, [any arguments])
This method creates a new Window object, sets its
dialogArguments
property to
arguments
,
loads
url
into the window, and blocks until the window is closed. Once closed, it returns the
returnValue
property of the window. See §14.5 and Example 14-4 for a discussion and
example.
Event Handlers
Most events that occur on HTML elements bubble up the document tree, to the Document
object and then on to the Window object. For this reason, you can use all of the event handler
properties listed in
Element
on Window objects. And in addition, you can use the event handler
properties listed below. For historical reasons, each of the event handler properties listed here
can also be defined (as an HTML attribute or as a JavaScript property) on the
<body>
element.
Event Handler
Invoked...
onafterprint
After the window’s contents are printed
onbefore
print
Before the window’s contents are printed
onbe
foreunload
Before navigating away from the current page. If the return value is a string, or if the handler sets the
returnValue
property of its event object to a string, that string will be displayed in a confirmation
dialog. See
BeforeUnloadEvent
.
onblur
When the window loses keyboard focus
onerror
When a JavaScript error occurs. This is not an ordinary event handler. See §14.6.
onfocus
When the window gains keyboard focus
onhashchange
When the fragment identifier (see
Location.hash
) of the document changes as the result of history
navigation (see
HashChangeEvent
)
Window
Client-Side JavaScript Reference | 1007
Client-Side
JavaScript
Reference
VB.NET PDF: Basic SDK Concept of XDoc.PDF You may add PDF document protection functionality into your VB.NET program. to edit hyperlink of PDF document, including editing PDF url links and quick
convert excel to pdf with hyperlinks; add links in pdf
90
Event Handler
Invoked...
onload
When the document and its external resources are fully loaded
onmessage
When a script in another window sends a message by calling the
postMessage()
method. See
Mes-
sageEvent
.
onoffline
When the browser loses its connection to the Internet
ononline
When the browser regains a connection to the Internet
onpagehide
When the page is about to be cached and replaced by another page
onpageshow
When a page is first loaded, a pageshow event is fired right after the load event, and the event object has
a
persisted
property of
false
. When a previously loaded page is restored from the browser’s in-
memory cache, however, no load event is fired (since the cached page is already in its loaded state) and a
pageshow event is fired with an event object that has its
persisted
property set to true. See
Page-
TransitionEvent
.
onpopstate
When the browser loads a new page or restores a state saved with
History.pushState()
or
History.replaceState()
. See
PopStateEvent
.
onresize
When the user changes the size of the browser window
onscroll
When the user scrolls the browser window
onstorage
Content of
localStorage
or
sessionStorage
changes. See
StorageEvent
.
onunload
The browser navigates away from a page. Note that if you register an
onunload
handler for a page, that
page will not be cacheable. To allow users to quickly return to your page without reloading, use
onpagehide
instead.
Worker
a worker thread
EventTarget
A Worker represents a background thread. Create a new Worker with the
Worker()
con-
structor, passing the URL of a file of JavaScript code for it to run. The JavaScript code in that
file can use synchronous APIs or perform compute-intensive tasks without freezing up the
main UI thread. Workers run their code in a completely separate execution context (see
WorkerGlobalScope
), and the only way to exchange data with a worker is via asynchronous
events. Call
postMessage()
to send data to the Worker, and handle message events to receive
data from the worker.
See §22.4 for an introduction to worker threads.
Constructor
new Worker(string scriptURL)
Constructs a new Worker object and causes it to run the JavaScript code in
scriptURL
.
Methods
void postMessage(any message, [MessagePort[] ports])
Send
message
to the worker, which will receive it as a MessageEvent object sent to its
onmessage
handler.
message
can be a JavaScript primitive value or object or array, but not a
Worker
1008 | Client-Side JavaScript Reference
70
function. Client-side types ArrayBuffer, File, Blob, and ImageData are allowed, but Nodes,
such as Document and Element, are not allowed (see “Structured Clones” on page 672 for
details).
The optional
ports
argument is an advanced feature that allows you to pass one or more direct
communication channels to the Worker. If you create two Worker objects, for example, you
can allow them to communicate directly with each other by passing them each one end of a
MessageChannel
.
void terminate()
Stop the worker and abort the script it is running.
Event Handlers
Because workers run code in a completely separate execution environment than the one that
created them, the only way they can communicate with their parent thread is by events. You
can register event handlers on these properties or use the
EventTarget
methods.
onerror
When an exception is thrown in the script being run by a Worker, and that error is not
handled by the
onerror
handler of the WorkerGlobalScope, the error triggers an error
event on the Worker object. The event object associated with this event is a
ErrorEvent
. The error event does not bubble. If this worker is owned by another worker,
canceling an error event prevents it from being propagated up to the parent worker. If
this Worker object is already in the main thread, canceling the event may prevent it from
being displayed in the JavaScript console.
onmessage
When the script that the worker is running calls its global
postMessage()
function (see
WorkerGlobalScope
), a message event is triggered on the Worker object. The object passed
to the event handler is a
MessageEvent
, and its
data
property contains a clone of the value
that the worker script passed to
postMessage()
.
WorkerGlobalScope
EventTarget, Global
A Worker thread runs in a completely different execution environment than the parent thread
that spawned it. The global object for a worker is a WorkerGlobalScope object, so this page
describes the execution environment “inside” a Worker. Since WorkerGlobalScope is a global
object, it inherits the
Global
object of core JavaScript.
Properties
In addition to the properties listed here, WorkerGlobalScope also defines all of the core Java-
Script global properties, such as
Math
and
JSON
.
readonly WorkerLocation location
This property is like the
window.location
Location
object: it allows a worker to inspect
the URL it was loaded from and includes properties that return individual portions of
the URL.
WorkerGlobalScope
Client-Side JavaScript Reference | 1009
Client-Side
JavaScript
Reference
68
readonly WorkerNavigator navigator
This property is like the
window.navigator
Navigator
object: it defines properties that
allow a worker to determine what browser it is running in and whether it is currently
online or not.
readonly WorkerGlobalScope self
This self-referential property refers to the WorkerGlobalScope global object itself. It is
like the
window
property of the Window object in the main thread.
Methods
In addition to the properties listed here, WorkerGlobalScope also defines all of the core Java-
Script global functions, such as
isNaN()
and
eval()
.
void clearInterval(long handle)
This method is just like the Window method of the same name.
void clearTimeout(long handle)
This method is just like the Window method of the same name.
void close()
This method puts the worker into a special “closing” state. Once in this state, it will not fire
any timers or trigger any events. The script continues running until it returns to the worker’s
event loop, at which point the worker stops.
void importScripts(string urls...)
For each of the specified
urls
, this method resolves the URL relative to the worker’s
location
, then loads the content of the URL, and then executes that content as JavaScript
code. Note that this is a synchronous method. It loads and executes each file in turn and does
not return until all scripts have executed. (If any script throws an exception, however, that
exception will propagate and prevent any subsequent URLs from being loaded and executed.)
void postMessage(any message, [MessagePort[] ports])
Sends a
message
(and optionally an array of
ports
) to the thread that spawned this worker.
Calling this method causes a message event to be triggered on the Worker object in the parent
thread, and the associated MessageEvent object will include a clone of
message
as its
data
property. Note that in a worker,
postMessage()
is a global function.
long setInterval(any handler, [any timeout], any args...)
This method is just like the Window method of the same name.
long setTimeout(any handler, [any timeout], any args...)
This method is just like the Window method of the same name.
Constructors
WorkerGlobalScope includes all of the core JavaScript constructors, such as
Array()
,
Date()
, and
RegExp()
. It also defines key client-side constructors for
XMLHttpRequest
,
FileReaderSync
, and even the Worker object itself.
WorkerGlobalScope
1010 | Client-Side JavaScript Reference
67
Event Handlers
You can register event handlers for workers by setting these global event handler properties,
or you can use the
EventTarget
methods implemented by WorkerGlobalScope.
onerror
This is not a normal event handler: it is like the
onerror
property of Window rather than
the
onerror
property of Worker. When an unhandled exception occurs in the worker,
this function, if defined, will be invoked with three string arguments that specify an error
message, a script URL, and a line number. If the function returns
false
, the error is
considered handled and does not propagate. Otherwise, if this property is not set, or if
the error handler does not return
false
, the error propagates and causes an error event
on the Worker object in the parent thread.
onmessage
When the parent thread calls the
postMessage()
method of the Worker object that rep-
resents this worker, it causes a message event to be triggered on this WorkerGlobalScope.
This event handler function will be passed a MessageEvent object, and the
data
property
of that object will hold a clone of the
message
argument sent by the parent thread.
WorkerLocation
the URL of a worker’s main script
The WorkerLocation object referenced by the
location
property of a WorkerGlobalScope is
like the
Location
object referenced by the
location
property of a Window: it represents the
URL of the worker’s main script and defines properties that represent portions of that URL.
Workers differ from Windows in that they cannot be navigated or reloaded, so the properties
of a WorkerLocation object are read-only, and the object does not implement the methods
of the Location object.
The WorkerLocation object does not automatically convert to a string the way a regular
location object does. In a worker, you cannot simply write
location
when you mean
location.href
.
Properties
These properties have the same meanings as the same-named properties of the Location
object.
readonly string hash
The fragment identifier portion of the URL, including the leading hash mark.
readonly string host
The host and port portions of the URL.
readonly string hostname
The host portion of the URL.
WorkerLocation
Client-Side JavaScript Reference | 1011
Client-Side
JavaScript
Reference
Download from Wow! eBook <www.wowebook.com>
50
readonly string href
The complete text of the URL that was passed to the
Worker()
constructor. This is the
only value that the worker receives directly from its parent thread: all other values are
received indirectly through message events.
readonly string pathname
The pathname portion of the URL.
readonly string port
The port portion of the URL.
readonly string protocol
The protocol portion of the URL.
readonly string search
The search or query portion of the URL, including the leading question mark.
WorkerNavigator
browser information for workers
The
navigator
property of a WorkerGlobalScope refers to a WorkerNavigator object that is
a simplified version of the
Navigator
object of a Window.
Properties
Each of these properties has the same meaning that it does in the Navigator object.
readonly string appName
See the
appName
property of Navigator.
readonly string appVersion
See the
appVersions
property of Navigator.
readonly boolean onLine
true
if the browser is online and
false
if it is not.
readonly string platform
A string that identifies the operating system and/or hardware platform on which the
browser is running.
readonly string userAgent
The value the browser uses for the user-agent header in HTTP requests.
XMLHttpRequest
An HTTP request and response
EventTarget
The XMLHttpRequest object allows client-side JavaScript to issue HTTP requests and receive
responses (which need not be XML) from web servers. XMLHttpRequest is the subject of
Chapter 18, and that chapter contains many examples of its use.
WorkerNavigator
1012 | Client-Side JavaScript Reference
Documents you may be interested
Documents you may be interested