From 85a88000a785b6c8941ec33b8b01db9f846ca2ea Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 5 Apr 2021 11:24:48 +0200 Subject: [PATCH] Fix "onload" events to support multiple --- articles/static/edit-keymap.js | 13 +++++++++++-- articles/static/live-preview.js | 12 ++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/articles/static/edit-keymap.js b/articles/static/edit-keymap.js index bf2a8d4..72ec61d 100644 --- a/articles/static/edit-keymap.js +++ b/articles/static/edit-keymap.js @@ -1,13 +1,22 @@ 'use strict'; -window.onload = function () { + +function bindKey() { const adminLinkElement = document.querySelector("a#admin-link"); if (adminLinkElement === undefined || adminLinkElement === null) { return; } const adminLocation = adminLinkElement.href; - document.addEventListener("keydown", function(event) { + document.addEventListener("keydown", function (event) { if (event.code === "KeyE") { window.location = adminLocation; } }) } + +((readyState) => { + if (readyState === "interactive") { + bindKey(); + } else if (readyState === "loading") { + window.addEventListener("DOMContentLoaded", bindKey, false); + } +})(document.readyState); diff --git a/articles/static/live-preview.js b/articles/static/live-preview.js index 17c1cad..8e7c370 100644 --- a/articles/static/live-preview.js +++ b/articles/static/live-preview.js @@ -1,11 +1,19 @@ let preview = null; -window.onload = function () { +function onLoad () { const previewButton = document.querySelector("input#_live_preview"); if (previewButton) { previewButton.addEventListener("click", openPreviewPopup); } -}; +} +((readyState) => { + if (readyState === "interactive") { + onLoad(); + } else if (readyState === "loading") { + window.addEventListener("DOMContentLoaded", onLoad, false); + } +})(document.readyState); + window.onbeforeunload = function () { if (preview !== null) { preview.close();