Quellcode durchsuchen

【修复】首页无参数时跳转其它文档页不触发 Pjax 的问题

PJ568 vor 2 Jahren
Ursprung
Commit
37409b9a5f
1 geänderte Dateien mit 12 neuen und 8 gelöschten Zeilen
  1. 12 8
      index.html

+ 12 - 8
index.html

@@ -3,7 +3,7 @@
 <!-- 作者:PJ568 -->
 <!-- 项目地址:https://github.com/PJ-568/markdown.html/ -->
 <!-- 本项目遵循 CC BY-SA 4.0 International 许可协议 -->
-<!-- v 0.3.0 -->
+<!-- v 0.3.1 -->
 
 <head>
   <meta charset="utf-8">
@@ -244,15 +244,15 @@
           document.getElementById('c').innerHTML = content;
           const firstH1 = document.querySelector('h1');
           document.title = firstH1 ? firstH1.textContent : absolutePath;
-          infoTranslate();	//// 检查并提示翻译
+          showMarkdownEnded();
         } catch (error) {
-          document.getElementById('c').innerHTML = '<h1>加载失败</h1><p><a href="">重试</a></p>';
+          document.getElementById('c').innerHTML = '<h1>加载失败</h1><p>文档可能不存在。您可以尝试<a href="">重试</a>。</p>';
           console.error(error);
         }
       };
 
-      // 初始化自定义链接 Pjax
-      function _initContentPjax() {
+      // 初始化自定义 Pjax
+      function initContentPjax() {
         document.querySelector('#c').addEventListener('click', function (event) {
           const link = event.target.closest('a[href$=".md"]');
           if (link) {
@@ -260,8 +260,7 @@
               event.preventDefault();
               startLoad();
               showMarkdown(decodePath(link.href));
-              history.pushState(null, null, link.href);
-              endLoad();
+              history.pushState({ url: link.href, title: document.title, uid: link.href }, null, link.href);
             } catch (e) {
               console.error('自定义 Pjax 出错:' + e);
             }
@@ -296,7 +295,6 @@
       // 初始化
       function initialize() {
         initDarkmode();	//// 初始化深色模式
-        // initContentPjax();	//// 初始化自定义链接 Pjax
         initHomeBtn();	//// 初始化返回首页按钮
         initContent();	//// 初始化内容
         initTranslate();	//// 初始化页面翻译
@@ -322,6 +320,12 @@
         translate.execute();    // 手动触发翻译,但是理论上 translate-js 会监听页面变化
         endLoad();
       })
+      //// 文档内容加载完毕时触发
+      function showMarkdownEnded() {
+        infoTranslate();	//// 检查并提示翻译
+        initContentPjax();	//// 初始化自定义 Pjax
+        endLoad();
+      }
     })();
   </script>
   <title>加载中</title>