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);