const routes = { page1: "pages/page1.html", page2: "pages/page2.html", page3: "pages/page3.html", page4: "pages/page4.html", page5: "pages/page5.html", page6: "pages/page6.html", page7: "pages/page7.html", page8: "pages/page8.html", }; function clearOldScripts() { document.querySelectorAll("script[data-dynamic='1']").forEach(s => s.remove()); } function runScripts(container) { const scripts = container.querySelectorAll("script"); scripts.forEach(oldScript => { const s = document.createElement("script"); s.dataset.dynamic = "1"; if (oldScript.src) { s.src = oldScript.src; } else { s.textContent = oldScript.textContent; } document.body.appendChild(s); }); } async function loadPage(name) { const view = document.getElementById("view"); const url = routes[name]; if (!url) { view.innerHTML = "
无法找到页面
"; return; } try { const res = await fetch(url, { credentials: "include" }); const html = await res.text(); if (html.includes("login.html") || html.includes("登录")) { window.location.href = "/"; return; } const temp = document.createElement("div"); temp.innerHTML = html; clearOldScripts(); view.innerHTML = ""; view.appendChild(temp); runScripts(temp); switch (name) { case "page1": break; case "page2": initPage2(); break; case "page3": break; case "page4": break; case "page5": break; case "page6": break; case "page7": break; case "page8": break; } } catch (e) { console.error("loadPage error:", e); view.innerHTML = "
页面加载失败
"; } }