3 ms·
I think ”persist the input in the URL query string” could also be doable with static assets alone. The last <script> block in foo.html would need to be replaced
by divbzero 4y ago
I think ”persist the input in the URL query string” could also be doable with static assets alone. The last <script> block in foo.html would need to be replaced with something like:
<script>
// calculate output and update query string when any input changes
a.oninput = b.oninput = c.oninput = function () {
z.value = foo(
Number(a.value),
Number(b.value),
Number(c.value),
);
var params = new URLSearchParams();
if (a.value) params.set('a', a.value);
if (b.value) params.set('b', b.value);
if (c.value) params.set('c', c.value);
history.replaceState(null, '', '?' + params);
};
// fill in input from query string when page is loaded
var params = new URLSearchParams(location.search);
a.value = params.get('a');
b.value = params.get('b');
c.value = params.get('c');
a.oninput();
</script>