A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://www.w3.org/TR/html5/document-lifecycle.html below:

HTML Standard

HTML

Living Standard — Last Updated 17 October 2025

← 7.4 Navigation and session historyTable of Contents7.6 Speculative loading →
    1. 7.5 Document lifecycle
      1. 7.5.1 Shared document creation infrastructure
      2. 7.5.2 Loading HTML documents
      3. 7.5.3 Loading XML documents
      4. 7.5.4 Loading text documents
      5. 7.5.5 Loading multipart/x-mixed-replace documents
      6. 7.5.6 Loading media documents
      7. 7.5.7 Loading a document for inline content that doesn't have a DOM
      8. 7.5.8 Finishing the loading process
      9. 7.5.9 Unloading documents
      10. 7.5.10 Destroying documents
      11. 7.5.11 Aborting a document load
7.5 Document lifecycle

When loading a document using one of the below algorithms, we use the following steps to create and initialize a Document object, given a type, contentType, and navigationParams:

objects are also created when ; such are never created by this algorithm. Also, -less objects can be created via various APIs, such as .

  1. Let browsingContext be the result of given navigationParams.

    This can result in a browsing context group switch, in which case browsingContext will be a instead of being navigationParams's 's . In such a case, the created , , and will not end up being used; because the created 's is , we will end up creating a new and later in this algorithm to go along with the new .

  2. Let permissionsPolicy be the result of given navigationParams's 's , navigationParams's , and navigationParams's . [PERMISSIONSPOLICY]

    The algorithm makes use of the passed . If has been used for navigationParams's 's , then its cannot be with the passed origin, because these steps run before the document is created, so it cannot itself yet have used . Note that this means that Permissions Policy checks are less permissive compared to doing a check instead.

    See below for some examples of this in action.

  3. Let creationURL be navigationParams's 's .

  4. If navigationParams's is non-null, then set creationURL to navigationParams's 's .

  5. Let window be null.

  6. If browsingContext's 's is true, and browsingContext's 's is with navigationParams's , then set window to browsingContext's .

    This means that both the , and the new that is about to be created, will share the same object.

  7. Otherwise:

    1. Let oacHeader be the result of given `` and "item" from navigationParams's 's .

    2. Let requestsOAC be true if oacHeader is not null and oacHeader[0] is the true; otherwise false.

    3. If navigationParams's is a , then set requestsOAC to false.

    4. Let agent be the result of given navigationParams's , browsingContext's , and requestsOAC.

    5. Let realmExecutionContext be the result of given agent and the following customizations:

      • For the global object, create a new object.

      • For the global this binding, use browsingContext's object.

    6. Set window to the of realmExecutionContext's Realm component.

    7. Let topLevelCreationURL be creationURL.

    8. Let topLevelOrigin be navigationParams's .

    9. If navigable's is not null, then:

      1. Let parentEnvironment be navigable's 's .

      2. Set topLevelCreationURL to parentEnvironment's .

      3. Set topLevelOrigin to parentEnvironment's .

    10. with creationURL, realmExecutionContext, navigationParams's , topLevelCreationURL, and topLevelOrigin.

    This is the usual case, where the new we're about to create gets a new to go along with it.

  8. Let loadTimingInfo be a new with its set to navigationParams's 's 's .

  9. Let document be a new , with

    type
    contentType
    navigationParams's
    browsingContext
    navigationParams's
    permissionsPolicy
    navigationParams's
    navigationParams's
    loadTimingInfo
    navigationParams's 's
    navigationParams's
    creationURL
    "loading"
    navigationParams's
    true
    a new object
  10. Set window's to document.

  11. given document. [CSP]

  12. If navigationParams's is non-null, then:

    1. Set document's to the empty string.

    2. Let referrer be navigationParams's 's .

    3. If referrer is a , then set document's to the of referrer.

      Per Fetch, referrer will be either a or "no-referrer" at this point.

  13. If navigationParams's is not null, then:

    1. Let fullTimingInfo be the result of from navigationParams's .

    2. Let redirectCount be 0 if navigationParams's 's is true; otherwise navigationParams's 's .

    3. for document, given fullTimingInfo, redirectCount, navigationTimingType, navigationParams's 's , and navigationParams's 's .

  14. for document, with navigationParams's 's , redirectCount, navigationParams's , and navigationParams's 's .

  15. If navigationParams's has a `` header, then:

    1. Let value be the of the value of the header.

    2. Run the with document and value.

    We do not currently have a spec for how to handle multiple `` headers. This is tracked as issue #2900.

  16. If navigationParams's is not null, then call navigationParams's with document.

  17. given document, navigationParams's , and "pre-media".

  18. If navigationParams's is a , then given document and navigationParams's .

    This is conditional because speculative loads are only considered for top-level traversables, so it would be wasteful to fetch these rules otherwise.

  19. for document.

  20. Return document.

In this example, the child document is not allowed to use , despite being at the time the child document tries to use it. At the time the child document is initialized, only the parent document has set , and the child document has not.

<!-- https://foo.example.com/a.html -->
<!doctype html>
<script>
document.domain = 'example.com';
</script>
<iframe src=b.html></iframe>
<!-- https://bar.example.com/b.html -->
<!doctype html>
<script>
document.domain = 'example.com'; // This happens after the document is initialized
new PaymentRequest(…); // Not allowed to use
</script>

In this example, the child document is allowed to use , despite not being at the time the child document tries to use it. At the time the child document is initialized, none of the documents have set yet so falls back to a normal check.

<!-- https://example.com/a.html -->
<!doctype html>
<iframe src=b.html></iframe>
<!-- The child document is now initialized, before the script below is run. -->
<script>
document.domain = 'example.com';
</script>
<!-- https://example.com/b.html -->
<!doctype html>
<script>
new PaymentRequest(…); // Allowed to use
</script>

To populate with html/head/body given a document:

  1. Let html be the result of given document, "html", and the .

  2. Let head be the result of given document, "head", and the .

  3. Let body be the result of given document, "body", and the .

  4. html to document.

  5. head to html.

  6. body to html.

7.5.2 Loading HTML documents

To load an HTML document, given navigation params navigationParams:

  1. Let document be the result of creating and initializing a Document object given "html", "text/html", and navigationParams.

  2. If document's URL is about:blank, then populate with html/head/body given document.

    This special case, where even non-initial about:blank Documents are synchronously given their element nodes, is necessary for compatible with deployed content. In other words, it is not compatible to instead go down the "otherwise" branch and feed the empty byte sequence into an HTML parser to asynchronously populate document.

  3. Otherwise, create an HTML parser and associate it with the document. Each task that the networking task source places on the task queue while fetching runs must then fill the parser's input byte stream with the fetched bytes and cause the HTML parser to perform the appropriate processing of the input stream.

    The first task that the networking task source places on the task queue while fetching runs must process link headers given document, navigationParams's response, and "media", after the task has been processed by the HTML parser.

    Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be true for document.

    The input byte stream converts bytes into characters for use in the tokenizer. This process relies, in part, on character encoding information found in the real Content-Type metadata of the resource; the computed type is not used for this purpose.

    When no more bytes are available, the user agent must queue a global task on the networking task source given document's relevant global object to have the parser process the implied EOF character, which eventually causes a load event to be fired.

  4. Return document.

7.5.3 Loading XML documents

When faced with displaying an XML file inline, provided navigation params navigationParams and a string type, user agents must follow the requirements defined in XML and Namespaces in XML, XML Media Types, DOM, and other relevant specifications to create and initialize a Document object document, given "xml", type, and navigationParams, and return that Document. They must also create a corresponding XML parser. [XML] [XMLNS] [RFC7303] [DOM]

At the time of writing, the XML specification community had not actually yet specified how XML and the DOM interact.

The first task that the networking task source places on the task queue while fetching runs must process link headers given document, navigationParams's response, and "media", after the task has been processed by the XML parser.

The actual HTTP headers and other metadata, not the headers as mutated or implied by the algorithms given in this specification, are the ones that must be used when determining the character encoding according to the rules given in the above specifications. Once the character encoding is established, the document's character encoding must be set to that character encoding.

Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be true for the newly-created Document.

Once parsing is complete, the user agent must set document's during-loading navigation ID for WebDriver BiDi to null.

For HTML documents this is reset when parsing is complete, after firing the load event.

Error messages from the parse process (e.g., XML namespace well-formedness errors) may be reported inline by mutating the Document.

7.5.4 Loading text documents

To load a text document, given a navigation params navigationParams and a string type:

  1. Let document be the result of creating and initializing a Document object given "html", type, and navigationParams.

  2. Set document's parser cannot change the mode flag to true.

  3. Set document's mode to "no-quirks".

  4. Create an HTML parser and associate it with the document. Act as if the tokenizer had emitted a start tag token with the tag name "pre" followed by a single U+000A LINE FEED (LF) character, and switch the HTML parser's tokenizer to the PLAINTEXT state. Each task that the networking task source places on the task queue while fetching runs must then fill the parser's input byte stream with the fetched bytes and cause the HTML parser to perform the appropriate processing of the input stream.

    document's encoding must be set to the character encoding used to decode the document during parsing.

    The first task that the networking task source places on the task queue while fetching runs must process link headers given document, navigationParams's response, and "media", after the task has been processed by the HTML parser.

    Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be true for document.

    When no more bytes are available, the user agent must queue a global task on the networking task source given document's relevant global object to have the parser process the implied EOF character, which eventually causes a load event to be fired.

  5. User agents may add content to the head element of document, e.g., linking to a style sheet, providing script, or giving the document a title.

    In particular, if the user agent supports the Format=Flowed feature of RFC 3676 then the user agent would need to apply extra styling to cause the text to wrap correctly and to handle the quoting feature. This could be performed using, e.g., a CSS extension.

  6. Return document.

The rules for how to convert the bytes of the plain text document into actual characters, and the rules for actually rendering the text to the user, are defined by the specifications for the computed MIME type of the resource (i.e., type).

7.5.5 Loading multipart/x-mixed-replace documents

For the purposes of algorithms processing these body parts as if they were complete stand-alone resources, the user agent must act as if there were no more bytes for those resources whenever the boundary following the body part is reached.

Thus, load events (and for that matter unload events) do fire for each body part loaded.

7.5.6 Loading media documents

To load a media document, given navigationParams and a string type:

  1. Let document be the result of creating and initializing a Document object given "html", type, and navigationParams.

  2. Set document's mode to "no-quirks".

  3. Populate with html/head/body given document.

  4. Append an element host element for the media, as described below, to the body element.

  5. Set the appropriate attribute of the element host element, as described below, to the address of the image, video, or audio resource.

  6. User agents may add content to the head element of document, or attributes to host element, e.g., to link to a style sheet, to provide a script, to give the document a title, or to make the media autoplay.

  7. Process link headers given document, navigationParams's response, and "media".

  8. Act as if the user agent had stopped parsing document.

  9. Return document.

The element host element to create for the media is the element given in the table below in the second cell of the row whose first cell describes the media. The appropriate attribute to set is the one given by the third cell in that same row.

Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be true for the Document.

7.5.7 Loading a document for inline content that doesn't have a DOM

When the user agent is to create a document to display a user agent page or PDF viewer inline, provided a navigable navigable, a navigation ID navigationId, a NavigationTimingType navTimingType, and a user navigation involvement userInvolvement, the user agent should:

  1. Let origin be a new opaque origin.

  2. Let coop be a new opener policy.

  3. Let coopEnforcementResult be a new opener policy enforcement result with

    url
    response's URL
    origin
    origin
    opener policy
    coop
  4. Let navigationParams be a new navigation params with

    id
    navigationId
    navigable
    navigable
    request
    null
    response
    a new response
    origin
    origin
    fetch controller
    null
    commit early hints
    null
    COOP enforcement result
    coopEnforcementResult
    reserved environment
    null
    policy container
    a new policy container
    final sandboxing flag set
    an empty set
    opener policy
    coop
    navigation timing type
    navTimingType
    about base URL
    null
    user involvement
    userInvolvement
  5. Let document be the result of creating and initializing a Document object given "html", "text/html", and navigationParams.

  6. Either associate document with a custom rendering that is not rendered using the normal Document rendering rules, or mutate document until it represents the content the user agent wants to render.

  7. Act as if the user agent had stopped parsing document.

  8. Return document.

Because we ensure the resulting Document's origin is opaque, and the resulting Document cannot run script with access to the DOM, the existence and properties of this Document are not observable to web developer code. This means that most of the above values, e.g., the text/html type, do not matter. Similarly, most of the items in navigationParams don't have any observable effect, besides preventing the Document-creation algorithm from getting confused, and so are set to default values.

7.5.8 Finishing the loading process

A Document has a completely loaded time (a time or null), which is initially null.

A Document is considered completely loaded if its completely loaded time is non-null.

7.5.9 Unloading documents

A Document has a salvageable state, which must initially be true, and a page showing boolean, which is initially false. The page showing boolean is used to ensure that scripts receive pageshow and pagehide events in a consistent manner (e.g., that they never receive two pagehide events in a row without an intervening pageshow, or vice versa).

A Document has a DOMHighResTimeStamp suspension time, initially 0.

A Document has a list of suspended timer handles, initially empty.

Event loops have a termination nesting level counter, which must initially be 0.

Document objects have an unload counter, which is used to ignore certain operations while the below algorithms run. Initially, the counter must be set to zero.

To unload a Document oldDocument, given an optional Document newDocument:

  1. Assert: this is running as part of a task queued on oldDocument's relevant agent's event loop.

  2. Let unloadTimingInfo be a new document unload timing info.

  3. If newDocument is not given, then set unloadTimingInfo to null.

    In this case there is no new document that needs to know about how long it took oldDocument to unload.

  4. Otherwise, if newDocument's event loop is not oldDocument's event loop, then the user agent may be unloading oldDocument in parallel. In that case, the user agent should set unloadTimingInfo to null.

    In this case newDocument's loading is not impacted by how long it takes to unload oldDocument, so it would be meaningless to communicate that timing info.

  5. Let intendToKeepInBfcache be true if the user agent intends to keep oldDocument alive in a session history entry, such that it can later be used for history traversal.

    This must be false if oldDocument is not salvageable, or if there are any descendants of oldDocument which the user agent does not intend to keep alive in the same way (including due to their lack of salvageability).

  6. Let eventLoop be oldDocument's relevant agent's event loop.

  7. Increase eventLoop's termination nesting level by 1.

  8. Increase oldDocument's unload counter by 1.

  9. If intendToKeepInBfcache is false, then set oldDocument's salvageable state to false.

  10. If oldDocument's page showing is true:

    1. Set oldDocument's page showing to false.

    2. Fire a page transition event named pagehide at oldDocument's relevant global object with oldDocument's salvageable state.

    3. Update the visibility state of oldDocument to "hidden".

  11. If unloadTimingInfo is not null, then set unloadTimingInfo's unload event start time to the current high resolution time given newDocument's relevant global object, coarsened given oldDocument's relevant settings object's cross-origin isolated capability.

  12. If oldDocument's salvageable state is false, then fire an event named unload at oldDocument's relevant global object, with legacy target override flag set.

  13. If unloadTimingInfo is not null, then set unloadTimingInfo's unload event end time to the current high resolution time given newDocument's relevant global object, coarsened given oldDocument's relevant settings object's cross-origin isolated capability.

  14. Decrease eventLoop's termination nesting level by 1.

  15. Set oldDocument's suspension time to the current high resolution time given document's relevant global object.

  16. Set oldDocument's suspended timer handles to the result of getting the keys for the map of active timers.

  17. Set oldDocument's has been scrolled by the user to false.

  18. Run any unloading document cleanup steps for oldDocument that are defined by this specification and other applicable specifications.

  19. If oldDocument's node navigable is a top-level traversable, build not restored reasons for a top-level traversable and its descendants given oldDocument's node navigable.

  20. If oldDocument's salvageable state is false, then destroy oldDocument.

  21. Decrease oldDocument's unload counter by 1.

  22. If newDocument is given, newDocument's was created via cross-origin redirects is false, and newDocument's origin is the same as oldDocument's origin, then set newDocument's previous document unload timing to unloadTimingInfo.

To unload a document and its descendants, given a Document document, an optional Document-or-null newDocument (default null), an optional set of steps afterAllUnloads, and an optional set of steps firePageSwapSteps:

  1. Assert: this is running within document's node navigable's traversable navigable's session history traversal queue.

  2. Let childNavigables be document's child navigables.

  3. Let numberUnloaded be 0.

  4. For each childNavigable of childNavigables in what order?, queue a global task on the navigation and traversal task source given childNavigable's active window to perform the following steps:

    1. Let incrementUnloaded be an algorithm step which increments numberUnloaded.

    2. Unload a document and its descendants given childNavigable's active document, null, and incrementUnloaded.

  5. Wait until numberUnloaded equals childNavigable's size.

  6. Queue a global task on the navigation and traversal task source given document's relevant global object to perform the following steps:

    1. If firePageSwapSteps is given, then run firePageSwapSteps.

    2. Unload document, passing along newDocument if it is not null.

    3. If afterAllUnloads was given, then run it.

It would be better if specification authors sent a pull request to add calls from here into their specifications directly, instead of using the unloading document cleanup steps hook, to ensure well-defined cross-specification call order. As of the time of this writing the following specifications are known to have unloading document cleanup steps, which will be run in an unspecified order: Fullscreen API, Web NFC, WebDriver BiDi, Compute Pressure, File API, Media Capture and Streams, Picture-in-Picture, Screen Orientation, Service Workers, WebLocks API, WebAudio API, WebRTC. [FULLSCREEN] [WEBNFC] [WEBDRIVERBIDI] [COMPUTEPRESSURE] [FILEAPI] [MEDIASTREAM] [PICTUREINPICTURE] [SCREENORIENTATION] [SW] [WEBLOCKS] [WEBAUDIO] [WEBRTC]

Issue #8906 tracks the work to make the order of these steps clear.

7.5.10 Destroying documents

Even after destruction, the Document object itself might still be accessible to script, in the case where we are destroying a child navigable.

7.5.11 Aborting a document load

Through their user interface, user agents also allow stopping traversals, i.e. cases where the ongoing navigation is "traversal". The above algorithm does not account for this. (On the other hand, user agents do not allow window.stop() to stop traversals, so the above algorithm is correct for that caller.) See issue #6905.

← 7.4 Navigation and session historyTable of Contents7.6 Speculative loading →

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.5