Document: prerenderingchange event
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The prerenderingchange
event is fired on a prerendered document when it is activated (i.e. the user views the page).
Syntax
Use the event name in methods like addEventListener()
, or set an event handler property.
addEventListener("prerenderingchange", (event) => {});
prerenderingchange = (event) => {};
Event type
A generic Event
.
Examples
The following code sets up an event listener to run a function once prerendering has finished, on a prerendered page (the prerendering is detected via Document.prerendering
), or runs it immediately on a non-prerendered page:
if (document.prerendering) {
document.addEventListener("prerenderingchange", initAnalytics, {
once: true,
});
} else {
initAnalytics();
}
Note: See the Speculation Rules API landing page and particularly the Unsafe speculative loading conditions section for more information on the kinds of activities you might wish to delay until after prerendering has finished.
Specifications
Specification |
---|
Prerendering Revamped # eventdef-document-prerenderingchange |
Prerendering Revamped # dom-document-onprerenderingchange |
Browser compatibility
BCD tables only load in the browser
See also
- Speculation Rules API
prerendering
property