This section describes features that apply most directly to Web browsers. Having said that, except where specified otherwise, the requirements defined in this section do apply to all user agents, whether they are Web browsers or not.
A browsing context is an environment in which
Document
objects are presented to the user.
A tab or window in a Web browser typically contains
a browsing context, as does an iframe
or frame
s in a
frameset
.
Each browsing context has a corresponding
WindowProxy
object.
A browsing context has a session
history, which lists the Document
objects that
that browsing context has presented, is presenting, or
will present. At any time, one Document
in each
browsing context is designated the active
document.
Each Document
is associated with a
Window
object. A browsing context's
WindowProxy
object forwards everything to the
browsing context's active document's
Window
object.
In general, there is a 1-to-1 mapping from the
Window
object to the Document
object. In
one particular case, a Window
can be reused for the
presentation of a second Document
in the same
browsing context, such that the mapping is then
2-to-1. This occurs when a browsing context is navigated from the initial
about:blank
Document
to another, with
replacement enabled.
A Document
does not necessarily have a
browsing context associated with it. In particular,
data mining tools are likely to never instantiate browsing
contexts.
A browsing context can have a creator browsing context, the browsing context that was responsible for its creation. If a browsing context has a parent browsing context, then that is its creator browsing context. Otherwise, if the browsing context has an opener browsing context, then that is its creator browsing context. Otherwise, the browsing context has no creator browsing context.
If a browsing context A has a
creator browsing context, then the
Document
that was the active document of
that creator browsing context at the time A was created is the creator
Document
.
When a browsing context is first created, it must be
created with a single Document
in its session history,
whose address is
about:blank
, which is marked as being an HTML document, and whose character encoding is
UTF-8. The Document
must have a single child
html
node, which itself has a single child
body
node.
If the browsing context is created specifically to be immediately navigated, then that initial navigation will have replacement enabled.
The origin of the
about:blank
Document
is set when the
Document
is created. If the new browsing
context has a creator browsing context, then the
origin of the about:blank
Document
is the origin of the
creator Document
. Otherwise, the
origin of the about:blank
Document
is a globally unique identifier assigned when
the new browsing context is created.
Certain elements (for example, iframe
elements) can
instantiate further browsing
contexts. These are called nested browsing contexts. If a browsing context P has a Document
s D
with an element E that nests another browsing
context C inside it, then C
is said to be nested
through D, and E is
said to be the browsing context container of C. If the browsing context container
element E is in the Document
D,
then P is said to be the parent browsing
context of C and C is
said to be a child browsing context of P. Otherwise, the nested browsing
context C has no parent browsing
context.
A browsing context A is said to be an ancestor of a browsing context B if there exists a browsing context A' that is a child browsing context of A and that is itself an ancestor of B, or if there is a browsing context P that is a child browsing context of A and that is the parent browsing context of B.
A browsing context that is not a nested browsing context has no parent browsing context, and is the top-level browsing context of all the browsing contexts for which it is an ancestor browsing context.
The transitive closure of parent browsing contexts for a nested browsing context gives the list of ancestor browsing contexts.
The list of the descendant browsing contexts of a
Document
d is the (ordered) list
returned by the following algorithm:
Let list be an empty list.
For each child browsing context of d that is nested through an element that is in the Document
d, in the tree order of the elements
nesting those browsing
contexts, run these substeps:
Append that child browsing context to the list list.
Append the list of the descendant browsing contexts of the active document of that child browsing context to the list list.
Return the constructed list.
A Document
is said to be fully active
when it is the active document of its browsing
context, and either its browsing context is a top-level
browsing context, or it has a parent browsing
context and the Document
through which it is nested is itself fully
active.
Because they are nested through an element, child browsing contexts are always tied to
a specific Document
in their parent browsing
context. User agents must not allow the user to interact with
child browsing contexts
of elements that are in Document
s that are not
themselves fully active.
A nested browsing context can have a seamless
browsing context flag set, if it is embedded through an
iframe
element with a seamless
attribute.
A nested browsing context can in some
cases be taken out of its parent browsing context (e.g.
if an iframe
element is removed from its
Document
). In such a situation, the nested
browsing context has no parent browsing context,
but it still has the same browsing context container
and is still nested
through that element's Document
. Such a
nested browsing context is not a
top-level browsing context, and cannot contain
Document
s that are fully active.
Furthermore, if a browsing context container (such as
an iframe
) is moved to another Document
,
then the parent browsing context of its nested
browsing context will change.
top
Returns the WindowProxy
for the top-level browsing context.
parent
Returns the WindowProxy
for the parent browsing context.
frameElement
Returns the Element
for the browsing context container.
Returns null if there isn't one.
Throws a SECURITY_ERR
exception in cross-origin situations.
The top
IDL attribute on
the Window
object of a Document
in a
browsing context b must return the
WindowProxy
object of its top-level browsing
context (which would be its own WindowProxy
object if it was a top-level browsing context itself),
if it has one, or its own WindowProxy
object otherwise
(e.g. if it was a detached nested browsing
context).
The parent
IDL
attribute on the Window
object of a
Document
in a browsing context b must return the WindowProxy
object of
the parent browsing context, if there is one (i.e. if
b is a child browsing context), or
the WindowProxy
object of the browsing
context b itself, otherwise (i.e. if it
is a top-level browsing context or a detached
nested browsing context).
The frameElement
IDL attribute on the Window
object of a
Document
d, on getting, must run
the following algorithm:
If d is not a Document
in a
nested browsing context, return null and abort these
steps.
If the browsing context container's
Document
does not have the same effective script origin as the
entry script, then throw a SECURITY_ERR
exception.
Otherwise, return the browsing context container for b.
It is possible to create new browsing contexts that are related to a top-level browsing context without being nested through an element. Such browsing contexts are called auxiliary browsing contexts. Auxiliary browsing contexts are always top-level browsing contexts.
An auxiliary browsing context has an opener browsing context, which is the browsing context from which the auxiliary browsing context was created.
The opener
IDL
attribute on the Window
object, on getting, must return
the WindowProxy
object of the browsing
context from which the current browsing context
was created (its opener browsing context), if there is
one, if it is still available, and if the current browsing
context has not disowned its opener. On setting, if
the new value is null then the current browsing context
must disown its opener; if
the new value is anything else then the user agent must ignore the
new value.
User agents may support secondary browsing contexts, which are browsing contexts that form part of the user agent's interface, apart from the main content area.
A browsing context A is allowed to navigate a second browsing context B if one of the following conditions is true:
An element has a browsing context scope origin if its
Document
's browsing context is a
top-level browsing context or if all of its
Document
's ancestor browsing contexts all have active documents whose
origin are the same origin as the
element's Document
's origin. If an element
has a browsing context scope origin, then its value is
the origin of the element's Document
.
Each browsing context is defined as having a list of one or more directly reachable browsing contexts. These are:
The transitive closure of all the browsing contexts that are directly reachable browsing contexts forms a unit of related browsing contexts.
Each unit of related browsing contexts is then
further divided into the smallest number of groups such that every
member of each group has an active document with an
effective script origin that, through appropriate
manipulation of the document.domain
attribute, could
be made to be the same as other members of the group, but could not
be made the same as members of any other group. Each such group is a
unit of related similar-origin browsing contexts.
Each unit of related similar-origin browsing contexts can have a entry script which is used to obtain, amongst other things, the script's base URL to resolve relative URLs used in scripts running in that unit of related similar-origin browsing contexts. Initially, there is no entry script. It is changed by the jump to a code entry-point algorithm.
There is at most one event loop per unit of related similar-origin browsing contexts.
Browsing contexts can have a browsing context name. By default, a browsing context has no name (its name is not set).
A valid browsing context name is any string with at least one character that does not start with a U+005F LOW LINE character. (Names starting with an underscore are reserved for special keywords.)
A valid browsing context name or keyword is any string
that is either a valid browsing context name or that is
an ASCII case-insensitive match for one of: _blank
, _self
, _parent
, or _top
.
The rules for choosing a browsing context given a browsing context name are as follows. The rules assume that they are being applied in the context of a browsing context.
If the given browsing context name is the empty string or _self
, then the chosen browsing context must be
the current one.
If the given browsing context name is _self
, then this is an explicit
self-navigation override, which overrides the behavior of
the seamless browsing context flag set by the seamless
attribute on
iframe
elements.
If the given browsing context name is _parent
, then the chosen browsing context must be
the parent browsing context of the current
one, unless there isn't one, in which case the chosen browsing
context must be the current browsing context.
If the given browsing context name is _top
, then the chosen browsing context must be the
top-level browsing context of the current one, if
there is one, or else the current browsing context.
If the given browsing context name is not _blank
and there exists a browsing context whose
name is the same as the
given browsing context name, and the current browsing context is
allowed to navigate that browsing context, and the
user agent determines that the two browsing contexts are related
enough that it is ok if they reach each other, then that browsing
context must be the chosen one. If there are multiple matching
browsing contexts, the user agent should select one in some
arbitrary consistent manner, such as the most recently opened,
most recently focused, or more closely related.
If the browsing context is chosen by this step to be the current browsing context, then this is also an explicit self-navigation override.
Otherwise, a new browsing context is being requested, and what happens depends on the user agent's configuration and/or abilities:
The user agent may offer to create a new top-level browsing context or reuse an existing top-level browsing context. If the user picks one of those options, then the designated browsing context must be the chosen one (the browsing context's name isn't set to the given browsing context name). Otherwise (if the user agent doesn't offer the option to the user, or if the user declines to allow a browsing context to be used) there must not be a chosen browsing context.
noreferrer
keywordA new top-level browsing context must be
created. If the given browsing context name is not _blank
, then the new top-level browsing context's
name must be the given browsing context name (otherwise, it has
no name). The chosen browsing context must be this new browsing
context.
If it is immediately navigated, then the navigation will be done with replacement enabled.
noreferrer
keyword doesn't
applyA new auxiliary browsing context must be
created, with the opener browsing context being the
current one. If the given browsing context name is not _blank
, then the new auxiliary browsing context's
name must be the given browsing context name (otherwise, it has
no name). The chosen browsing context must be this new browsing
context.
If it is immediately navigated, then the navigation will be done with replacement enabled.
The chosen browsing context is the current browsing context.
There must not be a chosen browsing context.
User agent implementors are encouraged to provide a way for users to configure the user agent to always reuse the current browsing context.
Window
object[ReplaceableNamedProperties] interface Window { // the current browsing context readonly attribute WindowProxy window; readonly attribute WindowProxy self; readonly attribute Document document; attribute DOMString name; [PutForwards=href] readonly attribute Location location; readonly attribute History history; [Replaceable] readonly attribute BarProp locationbar; [Replaceable] readonly attribute BarProp menubar; [Replaceable] readonly attribute BarProp personalbar; [Replaceable] readonly attribute BarProp scrollbars; [Replaceable] readonly attribute BarProp statusbar; [Replaceable] readonly attribute BarProp toolbar; void close(); void stop(); void focus(); void blur(); // other browsing contexts [Replaceable] readonly attribute WindowProxy frames; [Replaceable] readonly attribute unsigned long length; readonly attribute WindowProxy top; attribute WindowProxy opener; readonly attribute WindowProxy parent; readonly attribute Element frameElement; WindowProxy open(in optional DOMString url, in optional DOMString target, in optional DOMString features, in optional DOMString replace); getter WindowProxy (in unsigned long index); getter any (in DOMString name); // the user agent readonly attribute Navigator navigator; readonly attribute ApplicationCache applicationCache; // user prompts void alert(in DOMString message); boolean confirm(in DOMString message); DOMString prompt(in DOMString message, in optional DOMString default); void print(); any showModalDialog(in DOMString url, in optional any argument); // event handler IDL attributes attribute Function onabort; attribute Function onafterprint; attribute Function onbeforeprint; attribute Function onbeforeunload; attribute Function onblur; attribute Function oncanplay; attribute Function oncanplaythrough; attribute Function onchange; attribute Function onclick; attribute Function oncontextmenu; attribute Function oncuechange; attribute Function ondblclick; attribute Function ondrag; attribute Function ondragend; attribute Function ondragenter; attribute Function ondragleave; attribute Function ondragover; attribute Function ondragstart; attribute Function ondrop; attribute Function ondurationchange; attribute Function onemptied; attribute Function onended; attribute Function onerror; attribute Function onfocus; attribute Function onhashchange; attribute Function oninput; attribute Function oninvalid; attribute Function onkeydown; attribute Function onkeypress; attribute Function onkeyup; attribute Function onload; attribute Function onloadeddata; attribute Function onloadedmetadata; attribute Function onloadstart; attribute Function onmessage; attribute Function onmousedown; attribute Function onmousemove; attribute Function onmouseout; attribute Function onmouseover; attribute Function onmouseup; attribute Function onmousewheel; attribute Function onoffline; attribute Function ononline; attribute Function onpause; attribute Function onplay; attribute Function onplaying; attribute Function onpagehide; attribute Function onpageshow; attribute Function onpopstate; attribute Function onprogress; attribute Function onratechange; attribute Function onreadystatechange; attribute Function onredo; attribute Function onreset; attribute Function onresize; attribute Function onscroll; attribute Function onseeked; attribute Function onseeking; attribute Function onselect; attribute Function onshow; attribute Function onstalled; attribute Function onstorage; attribute Function onsubmit; attribute Function onsuspend; attribute Function ontimeupdate; attribute Function onundo; attribute Function onunload; attribute Function onvolumechange; attribute Function onwaiting; }; Window implements EventTarget;
window
frames
self
These attributes all return window.
document
Returns the active document.
defaultView
Returns the Window
object of the active document.
The window
, frames
, and self
IDL attributes must all
return the Window
object's browsing
context's WindowProxy
object.
The document
IDL
attribute must return the Document
object of the
Window
object's Document
's browsing
context's active document.
The defaultView
IDL
attribute of the HTMLDocument
interface must return the
Document
's browsing context's
WindowProxy
object, if there is one, or null
otherwise.
User agents must raise a
SECURITY_ERR
exception whenever any of the members of a
Window
object are accessed by scripts whose
effective script origin is not the same as the
Window
object's Document
's effective
script origin, with the following exceptions:
location
object
postMessage()
method
frames
attribute
When a script whose effective script origin is not
the same as the Window
object's Document
's
effective script origin attempts to access that
Window
object's methods or attributes, the user agent
must act as if any changes to the Window
object's
properties, getters, setters, etc, were not present.
For members that return objects (including function objects),
each distinct effective script origin that is not the
same as the Window
object's Document
's
effective script origin must be provided with a
separate set of objects. These objects must have the prototype chain
appropriate for the script for which the objects are created (not
those that would be appropriate for scripts whose script's
global object is the Window
object in
question).
open
( [ url [, target [, features [, replace ] ] ] ] )Opens a window to show url (defaults to
about:blank
), and returns it. The target argument gives the name of the new
window. If a window exists with that name already, it is
reused. The replace attribute, if true, means
that whatever page is currently open in that window will be
removed from the window's session history. The features argument is ignored.
name
[ = value ]Returns the name of the window.
Can be set, to change the name.
close
()Closes the window.
stop
()Cancels the document load.
The open()
method on
Window
objects provides a mechanism for navigating an existing browsing
context or opening and navigating an auxiliary browsing
context.
The method has four arguments, though they are all optional.
The first argument, url, must be a
valid non-empty URL for a page to load in the browsing
context. If no arguments are provided, or if the first argument is
the empty string, then the url argument defaults
to "about:blank
". The argument must be resolved to an absolute
URL (or an error), relative to the entry
script's base URL,
when the method is invoked.
The second argument, target, specifies the
name of the browsing
context that is to be navigated. It must be a valid browsing
context name or keyword. If fewer than two arguments are
provided, then the target argument defaults to the
value "_blank
".
The third argument, features, has no defined effect and is mentioned for historical reasons only. User agents may interpret this argument as instructions to set the size and position of the browsing context, but are encouraged to instead ignore the argument entirely.
The fourth argument, replace, specifies whether or not the new page will replace the page currently loaded in the browsing context, when target identifies an existing browsing context (as opposed to leaving the current page in the browsing context's session history). When three or fewer arguments are provided, replace defaults to false.
When the method is invoked, the user agent must first select a browsing context to navigate by applying the rules for choosing a browsing context given a browsing context name using the target argument as the name and the browsing context of the script as the context in which the algorithm is executed, unless the user has indicated a preference, in which case the browsing context to navigate may instead be the one indicated by the user.
For example, suppose there is a user agent that
supports control-clicking a link to open it in a new tab. If a user
clicks in that user agent on an element whose onclick
handler uses the window.open()
API to open a page in an
iframe, but, while doing so, holds the control key down, the user
agent could override the selection of the target browsing context to
instead target a new tab.
Then, the user agent must navigate the selected browsing context to the absolute URL (or error) obtained from resolving url earlier. If the replace is true or if the browsing context was just created as part of the rules for choosing a browsing context given a browsing context name, then replacement must be enabled. The navigation must be done with the browsing context of the entry script as the source browsing context.
The method must return the WindowProxy
object of the
browsing context that was navigated, or null if no
browsing context was navigated.
The name
attribute of
the Window
object must, on getting, return the current
name of the browsing context, and, on setting, set the
name of the browsing context to the new value.
The name gets reset when the browsing context is navigated to another domain.
The close()
method on Window
objects should, if the corresponding
browsing context A is an
auxiliary browsing context that was created by a script
(as opposed to by an action of the user), and if the browsing context of the
script that invokes the method
is allowed to navigate the browsing
context A, close the browsing
context A (and may discard it too).
The stop()
method
on Window
objects should, if there is an existing
attempt to navigate the browsing context
and that attempt is not currently running the unload a
document algorithm, cancel that navigation and any associated instances of
the fetch algorithm. Otherwise, it must
do nothing.
length
Returns the number of child browsing contexts.
Returns the indicated child browsing context.
The length
IDL
attribute on the Window
interface must return the
number of child browsing
contexts that are nested through elements that are in the Document
that is the
active document of that Window
object, if
that Window
's browsing context shares the
same event loop as the script's browsing
context of the entry script accessing the IDL
attribute; otherwise, it must return zero.
The supported property indices on the
Window
object at any instant are the numbers in the
range 0 .. n-1, where n is the number returned by the length
IDL attribute. If n is zero then there are no supported property
indices.
To determine the value of an indexed
property index of a Window
object, the user agent must return the WindowProxy
object of the indexth child browsing
context of the Document
that is nested through
an element that is in the
Document
, sorted in the tree order
of the elements nesting those browsing contexts.
These properties are the dynamic nested browsing context properties.
Window
objectReturns the indicated element or collection of elements.
The Window
interface supports named properties. The supported
property names at any moment consist of:
name
content attribute
for all a
, applet
, area
,
embed
, form
, frame
,
frameset
, iframe
, img
, and
object
elements in the active document
that have a name
content attribute, andid
content
attribute of any HTML element in
the active document with an id
content attribute.It is possible that this will change. Browser vendors are considering limiting this behaviour to quirks mode. Read more...
When the Window
object is indexed for property retrieval using a name name, then the user agent must return the value
obtained using the following steps:
Let elements be the list of named elements with the name name in the active document.
There will be at least one such element, by definition.
If elements contains an iframe
element, then return the WindowProxy
object of the
nested browsing context represented by the first such
iframe
element in tree order, and abort
these steps.
Otherwise, if elements has only one element, return that element and abort these steps.
Otherwise return an HTMLCollection
rooted at the
Document
node, whose filter matches only named elements with
the name name.
Named elements with the name name, for the purposes of the above algorithm, are those that are either:
A browsing context has a strong reference to each of
its Document
s and its WindowProxy
object,
and the user agent itself has a strong reference to its top-level browsing
contexts.
A Document
has a strong reference to its
Window
object.
A Window
object has a strong reference to its
Document
object through its document
attribute. Thus, references
from other scripts to either of those objects will keep both
alive. Similarly, both Document
and Window
objects have implied strong
references to the WindowProxy
object.
Each script has a strong reference to its browsing context and its document.
When a browsing context is to discard a
Document
, the user agent must run the following
steps:
Set the Document
's salvageable state to
false.
Run any unloading document cleanup steps for
the Document
that are defined by this specification
and other applicable specifications.
Remove any tasks
associated with the Document
in any task
source, without running those tasks.
Discard
all the child browsing
contexts of the Document
.
Lose the strong reference from the Document
's
browsing context to the
Document
.
Whenever a Document
object is discarded, it is also removed from
the list of the worker's Document
s of each
worker whose list contains that Document
.
When a browsing context is
discarded, the strong reference from the user agent itself to
the browsing context must be severed, and all the
Document
objects for all the entries in the
browsing context's session history must be discarded as well.
User agents may discard top-level browsing contexts at any time (typically,
in response to user requests, e.g. when a user closes a window
containing one or more top-level browsing contexts). Other browsing contexts must be discarded
once their WindowProxy
object is eligible for garbage
collection.
To allow Web pages to integrate with Web browsers, certain Web browser interface elements are exposed in a limited way to scripts in Web pages.
Each interface element is represented by a BarProp
object:
interface BarProp { attribute boolean visible; };
locationbar
. visible
Returns true if the location bar is visible; otherwise, returns false.
menubar
. visible
Returns true if the menu bar is visible; otherwise, returns false.
personalbar
. visible
Returns true if the personal bar is visible; otherwise, returns false.
scrollbars
. visible
Returns true if the scroll bars are visible; otherwise, returns false.
statusbar
. visible
Returns true if the status bar is visible; otherwise, returns false.
toolbar
. visible
Returns true if the toolbar is visible; otherwise, returns false.
The visible attribute, on getting, must return either true or a value determined by the user agent to most accurately represent the visibility state of the user interface element that the object represents, as described below. On setting, the new value must be discarded.
The following BarProp
objects exist for each
Document
object in a browsing
context. Some of the user interface elements represented by
these objects might have no equivalent in some user agents; for
those user agents, except when otherwise specified, the object must
act as if it was present and visible (i.e. its visible
attribute must return
true).
BarProp
objectBarProp
objectBarProp
objectBarProp
objectBarProp
objectvisible
attribute may return false).BarProp
objectvisible
attribute may return false).The locationbar
attribute must return the location bar BarProp
object.
The menubar
attribute must return the menu bar BarProp
object.
The personalbar
attribute must return the personal bar BarProp
object.
The scrollbars
attribute must return the scrollbar BarProp
object.
The statusbar
attribute
must return the status bar BarProp
object.
The toolbar
attribute must return the toolbar BarProp
object.
WindowProxy
objectAs mentioned earlier, each browsing context has a
WindowProxy
object. This object is unusual
in that all operations that would be performed on it must be
performed on the Window
object of the browsing
context's active document instead. It is thus
indistinguishable from that Window
object in every way
until the browsing context is navigated.
There is no WindowProxy
interface object.
The WindowProxy
object allows scripts
to act as if each browsing context had a single
Window
object, while still keeping separate
Window
objects for each Document
.
In the following example, the variable x is
set to the WindowProxy
object returned by the window
accessor on the global object. All
of the expressions following the assignment return true, because in
every respect, the WindowProxy
object acts like the
underlying Window
object.
var x = window; x instanceof Window; // true x === this; // true