Spaces:
Running
Running
Fix critical bug: loadSession() reassigning answers.qXX orphaned live input bindings after a page reload (assignment3.html); harden assignment2.html the same way
Browse files- assignment2.html +5 -1
- assignment3.html +14 -6
assignment2.html
CHANGED
|
@@ -568,7 +568,11 @@ function loadSession(){
|
|
| 568 |
if (!d) return;
|
| 569 |
document.getElementById("studentId").value = d.studentId || "";
|
| 570 |
restoreRadio("q1", d.q1); restoreRadio("q2", d.q2); restoreRadio("q5", d.q5);
|
| 571 |
-
if (d.q3){
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
document.querySelectorAll("input.blank").forEach(inp => { const v = d.q3[inp.dataset.n]; if (v != null) inp.value = v; }); }
|
| 573 |
if (d.q4){
|
| 574 |
document.getElementById("q4change").value = d.q4.change || "";
|
|
|
|
| 568 |
if (!d) return;
|
| 569 |
document.getElementById("studentId").value = d.studentId || "";
|
| 570 |
restoreRadio("q1", d.q1); restoreRadio("q2", d.q2); restoreRadio("q5", d.q5);
|
| 571 |
+
if (d.q3){ Object.assign(answers.q3, d.q3);
|
| 572 |
+
// Mutate the EXISTING answers.q3 object in place rather than reassigning it --
|
| 573 |
+
// the blank inputs' oninput handlers closed over the original object by
|
| 574 |
+
// reference, so reassigning here would silently orphan that reference and
|
| 575 |
+
// freeze every future keystroke out of answers.q3 (and thus buildJSON()).
|
| 576 |
document.querySelectorAll("input.blank").forEach(inp => { const v = d.q3[inp.dataset.n]; if (v != null) inp.value = v; }); }
|
| 577 |
if (d.q4){
|
| 578 |
document.getElementById("q4change").value = d.q4.change || "";
|
assignment3.html
CHANGED
|
@@ -583,15 +583,23 @@ function loadSession(){
|
|
| 583 |
try { d = JSON.parse(localStorage.getItem(SESSION_KEY)); } catch(_){ return; }
|
| 584 |
if (!d) return;
|
| 585 |
document.getElementById("studentId").value = d.studentId || "";
|
| 586 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
answers.q12 = d.q12 || null; restoreRadio("q12", d.q12);
|
| 588 |
answers.q21 = d.q21 || null; restoreRadio("q21", d.q21);
|
| 589 |
answers.q22 = d.q22 || null; restoreRadio("q22", d.q22);
|
| 590 |
-
if (d.q3){
|
| 591 |
-
if (d.q41){
|
| 592 |
-
if (d.q43){
|
| 593 |
-
if (d.q44){
|
| 594 |
-
if (d.q44assert){
|
| 595 |
if (d.q42code) q42editor.setValue(d.q42code);
|
| 596 |
|
| 597 |
let ago = "earlier";
|
|
|
|
| 583 |
try { d = JSON.parse(localStorage.getItem(SESSION_KEY)); } catch(_){ return; }
|
| 584 |
if (!d) return;
|
| 585 |
document.getElementById("studentId").value = d.studentId || "";
|
| 586 |
+
// Mutate the EXISTING answers.qXX object/array in place (Object.assign(target, ...)
|
| 587 |
+
// / length=0+push) rather than reassigning answers.qXX to a brand-new one. The
|
| 588 |
+
// input/checkbox event handlers wired up earlier (buildBlanks, buildMulti) closed
|
| 589 |
+
// over the original object by reference; reassigning answers.qXX here would silently
|
| 590 |
+
// orphan that reference, so every future keystroke/click would write into the old,
|
| 591 |
+
// now-unreachable object while answers.qXX (and thus buildJSON()) stayed frozen at
|
| 592 |
+
// whatever was restored here -- exactly the bug that made Copy/Export appear to
|
| 593 |
+
// "stop updating" after a page reload.
|
| 594 |
+
if (d.q11){ answers.q11.length = 0; answers.q11.push(...d.q11); restoreCheckboxes("q11", d.q11); }
|
| 595 |
answers.q12 = d.q12 || null; restoreRadio("q12", d.q12);
|
| 596 |
answers.q21 = d.q21 || null; restoreRadio("q21", d.q21);
|
| 597 |
answers.q22 = d.q22 || null; restoreRadio("q22", d.q22);
|
| 598 |
+
if (d.q3){ Object.assign(answers.q3, d.q3); restoreBlanks(document.getElementById("q3block"), d.q3); }
|
| 599 |
+
if (d.q41){ Object.assign(answers.q41, d.q41); restoreBlanks(document.getElementById("q41block"), d.q41); }
|
| 600 |
+
if (d.q43){ Object.assign(answers.q43, d.q43); restoreBlanks(document.getElementById("q43block"), d.q43); }
|
| 601 |
+
if (d.q44){ Object.assign(answers.q44, d.q44); restoreBlanks(document.getElementById("q44block"), d.q44); }
|
| 602 |
+
if (d.q44assert){ Object.assign(answers.q44assert, d.q44assert); restoreBlanks(document.getElementById("q44assert"), d.q44assert); }
|
| 603 |
if (d.q42code) q42editor.setValue(d.q42code);
|
| 604 |
|
| 605 |
let ago = "earlier";
|