add pushState stuff for single.js

This commit is contained in:
Rhiannon Morris 2020-08-05 01:43:27 +02:00
parent 36ed5063ef
commit d6f7995f4b

View file

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