This feature is not Baseline because it does not work in some of the most widely-used browsers.
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).
Use the event name in methods like addEventListener(), or set an event handler property.
addEventListener("prerenderingchange", (event) => { })
onprerenderingchange = (event) => { }
A generic Event.
The example shows how to defer code, that would otherwise run during prerendering, until after page activation. This is useful for deferring analytics code, which is only relevant when and if the page is actually viewed.
The code checks if prerendering is running using Document.prerendering, and if so adds an event listener to run an analytics initialization function once the page is activated. On a page that is not prerendering the analytics code is run immediately.
if (document.prerendering) {
document.addEventListener("prerenderingchange", initAnalytics, {
once: true,
});
} else {
initAnalytics();
}
Note that this kind of code should not be used for measuring how often a prerender is activated, because the code may run after a prerendered page has already activated.
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.
This code shows how to measure how often a prerender is activated. It uses the prerenderingchange to track activation events, and Performance.getEntriesByType() to track navigation activations.
if (document.prerendering) {
document.addEventListener(
"prerenderingchange",
() => {
console.log("Prerender activated after this script ran");
},
{ once: true },
);
} else if (performance.getEntriesByType("navigation")[0]?.activationStart > 0) {
console.log("Prerender activated before this script ran");
} else {
console.log("This page load was not via prerendering");
}
| Specification |
|---|
| Prerendering Revamped> # eventdef-document-prerenderingchange> |
| Prerendering Revamped> # dom-document-onprerenderingchange> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
prerenderingchange_event |
108 | 108 | No | 94 | No | 108 | No | 73 | No | 21.0 | 108 | No |
prerendering propertyPerformanceNavigationTiming.activationStart property
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/prerenderingchange_event