From d6f7995f4b7600fcba06cfdf48e9ef3b2e494a00 Mon Sep 17 00:00:00 2001 From: Rhiannon Morris Date: Wed, 5 Aug 2020 01:43:27 +0200 Subject: [PATCH] add pushState stuff for single.js --- script/single.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/script/single.js b/script/single.js index ded26aa..8062c4f 100644 --- a/script/single.js +++ b/script/single.js @@ -46,9 +46,26 @@ function setImage(id, src, href, cw) { mainlink.href = href; } -function activateButton(button) { +function activateButton(button, doPush = true) { setImage(button.id, button.value, button.dataset.link, button.dataset.warning); + + if (doPush) history.pushState(null, '', '#' + button.id); +} + +function useFragment() { + let button = document.querySelector('#alts input'); + + let frag = document.location.hash.replace(/^#/, ''); + if (frag) { + let button0 = document.getElementById(frag); + if (button0) button = button0; + } + + if (button) { + button.checked = true; + activateButton(button, false); + } } function setup() { @@ -61,6 +78,9 @@ function setup() { } addCWListeners(document.getElementById('cw')); + + window.addEventListener('popstate', useFragment); + useFragment(); } window.addEventListener('DOMContentLoaded', setup);