router.js 808 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const routes = {
  2. page1: "pages/page1.html",
  3. page2: "pages/page2.html",
  4. page3: "pages/page3.html",
  5. page4: "pages/page4.html",
  6. page5: "pages/page5.html",
  7. page6: "pages/page6.html",
  8. page7: "pages/page7.html",
  9. page8: "pages/page8.html",
  10. };
  11. async function loadPage(name){
  12. const view = document.getElementById("view");
  13. const url = routes[name];
  14. if(!url){
  15. view.innerHTML = "<div class='card'>无法找到页面</div>";
  16. return;
  17. }
  18. try{
  19. const res = await fetch(url, {
  20. credentials: "include"
  21. });
  22. const html = await res.text();
  23. if (html.includes("login.html") || html.includes("登录")) {
  24. window.location.href = "/";
  25. return;
  26. }
  27. view.innerHTML = html;
  28. }catch(e){
  29. view.innerHTML = "<div class='card'>页面加载失败</div>";
  30. }
  31. }