From 70f9814506ff028c5efaa931512c05f07c5f0734 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Fri, 1 Nov 2024 13:48:31 +0900 Subject: [PATCH] Upate intro to "Preventing code from running during prerendering" Co-authored-by: Hamish Willee --- .../web/api/document/prerenderingchange_event/index.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/files/en-us/web/api/document/prerenderingchange_event/index.md b/files/en-us/web/api/document/prerenderingchange_event/index.md index 808bed965366f67..07d1e1ea521099e 100644 --- a/files/en-us/web/api/document/prerenderingchange_event/index.md +++ b/files/en-us/web/api/document/prerenderingchange_event/index.md @@ -30,7 +30,11 @@ A generic {{domxref("Event")}}. ### Preventing code from running during prerendering -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 {{domxref("Document.prerendering")}}), or runs it immediately on a non-prerendered page: +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 {{domxref("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. ```js if (document.prerendering) {