{"id":2147,"date":"2025-03-01T00:16:56","date_gmt":"2025-02-28T16:16:56","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2147"},"modified":"2025-03-01T00:16:56","modified_gmt":"2025-02-28T16:16:56","slug":"javascript-window-history-%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/01\/javascript-window-history-%e5%af%b9%e8%b1%a1\/","title":{"rendered":"JavaScript window.history \u5bf9\u8c61"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>window.history<\/code> \u5bf9\u8c61\u662f\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u7ba1\u7406\u63a5\u53e3\uff0c\u5b83\u5141\u8bb8\u4f60\u64cd\u4f5c\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u5806\u6808\uff0c\u5305\u62ec\u524d\u8fdb\u3001\u540e\u9000\u548c\u8df3\u8f6c\u5230\u7279\u5b9a\u7684\u5386\u53f2\u9875\u9762\u3002<code>history<\/code> \u5bf9\u8c61\u63d0\u4f9b\u4e86\u8bbf\u95ee\u548c\u4fee\u6539\u6d4f\u89c8\u5668\u4f1a\u8bdd\u5386\u53f2\u7684\u529f\u80fd\uff0c\u4f46\u5b83\u53ea\u80fd\u8bbf\u95ee\u540c\u4e00\u6765\u6e90\uff08\u540c\u4e00\u57df\u540d\u3001\u534f\u8bae\u548c\u7aef\u53e3\uff09\u7684\u5386\u53f2\u8bb0\u5f55\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>window.history<\/code> \u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>history.length<\/code><\/strong>\uff1a\u8fd4\u56de\u6d4f\u89c8\u5668\u5386\u53f2\u8bb0\u5f55\u5806\u6808\u4e2d\u7684\u6761\u76ee\u6570\u91cf\u3002\u8fd9\u4e2a\u6570\u5b57\u5305\u62ec\u5f53\u524d\u9875\u9762\u548c\u6240\u6709\u8bbf\u95ee\u8fc7\u7684\u9875\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  console.log(window.history.length);  \/\/ \u83b7\u53d6\u5386\u53f2\u8bb0\u5f55\u7684\u957f\u5ea6<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>window.history<\/code> \u5bf9\u8c61\u7684\u5e38\u7528\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>history.back()<\/code><\/strong>\uff1a\u8ba9\u6d4f\u89c8\u5668\u540e\u9000\u5230\u4e0a\u4e00\u4e2a\u9875\u9762\uff0c\u6548\u679c\u7b49\u540c\u4e8e\u70b9\u51fb\u6d4f\u89c8\u5668\u7684\u201c\u540e\u9000\u201d\u6309\u94ae\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  window.history.back();  \/\/ \u540e\u9000\u5230\u4e0a\u4e00\u4e2a\u9875\u9762<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>history.forward()<\/code><\/strong>\uff1a\u8ba9\u6d4f\u89c8\u5668\u524d\u8fdb\u5230\u4e0b\u4e00\u4e2a\u9875\u9762\uff0c\u6548\u679c\u7b49\u540c\u4e8e\u70b9\u51fb\u6d4f\u89c8\u5668\u7684\u201c\u524d\u8fdb\u201d\u6309\u94ae\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  window.history.forward();  \/\/ \u524d\u8fdb\u5230\u4e0b\u4e00\u4e2a\u9875\u9762<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>history.go()<\/code><\/strong>\uff1a\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6574\u6570\u53c2\u6570\uff0c\u6b63\u6570\u8868\u793a\u524d\u8fdb\uff0c\u8d1f\u6570\u8868\u793a\u540e\u9000\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u529f\u80fd\u6bd4 <code>back()<\/code> \u548c <code>forward()<\/code> \u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6307\u5b9a\u8df3\u8f6c\u5230\u5386\u53f2\u5806\u6808\u4e2d\u7684\u4efb\u610f\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  window.history.go(-1);  \/\/ \u540e\u90001\u4e2a\u9875\u9762\uff0c\u7b49\u540c\u4e8e back()\n  window.history.go(1);   \/\/ \u524d\u8fdb1\u4e2a\u9875\u9762\uff0c\u7b49\u540c\u4e8e forward()\n  window.history.go(0);   \/\/ \u5237\u65b0\u5f53\u524d\u9875\u9762<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>history.pushState()<\/code><\/strong>\uff1a\u5411\u6d4f\u89c8\u5668\u5386\u53f2\u8bb0\u5f55\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u72b6\u6001\u6761\u76ee\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4f1a\u5bfc\u81f4\u9875\u9762\u91cd\u65b0\u52a0\u8f7d\uff0c\u800c\u662f\u4ec5\u4ec5\u6539\u53d8\u6d4f\u89c8\u5668\u7684 URL\u3002\u5b83\u5e38\u7528\u4e8e\u5355\u9875\u5e94\u7528\uff08SPA\uff09\u7684\u5b9e\u73b0\uff0c\u5141\u8bb8\u6539\u53d8 URL \u800c\u4e0d\u5237\u65b0\u9875\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  history.pushState({ page: 1 }, 'Page 1', '\/page1');  \/\/ \u5411\u5386\u53f2\u8bb0\u5f55\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u6761\u76ee<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>history.replaceState()<\/code><\/strong>\uff1a\u66ff\u6362\u5f53\u524d\u5386\u53f2\u8bb0\u5f55\u4e2d\u7684\u6761\u76ee\u3002\u4e0e <code>pushState()<\/code> \u4e0d\u540c\uff0c<code>replaceState()<\/code> \u4f1a\u66ff\u6362\u5f53\u524d\u7684\u5386\u53f2\u8bb0\u5f55\u6761\u76ee\uff0c\u800c\u4e0d\u662f\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u6761\u76ee\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  history.replaceState({ page: 2 }, 'Page 2', '\/page2');  \/\/ \u66ff\u6362\u5f53\u524d\u5386\u53f2\u8bb0\u5f55\u6761\u76ee<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>pushState<\/code> \u548c <code>replaceState<\/code> \u65b9\u6cd5\u7684\u6df1\u5165\u8bb2\u89e3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pushState<\/code> \u548c <code>replaceState<\/code> \u662f HTML5 \u65b0\u589e\u7684\u65b9\u6cd5\uff0c\u5b83\u4eec\u7528\u4e8e\u6539\u53d8\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u72b6\u6001\uff0c\u4f46\u4e0d\u5f15\u8d77\u9875\u9762\u7684\u5237\u65b0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u5f00\u53d1\u5355\u9875\u5e94\u7528\uff08SPA\uff09\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u5141\u8bb8\u4f60\u5728\u4e0d\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u6539\u53d8 URL\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><code>pushState(state, title, url)<\/code><\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>state<\/code>\uff1a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4fdd\u5b58\u4e0e\u8be5\u5386\u53f2\u8bb0\u5f55\u6761\u76ee\u5173\u8054\u7684\u72b6\u6001\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>title<\/code>\uff1a\u901a\u5e38\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4f46\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u6587\u6863\u7684\u6807\u9898\u3002<\/li>\n\n\n\n<li><code>url<\/code>\uff1a\u8981\u5728\u6d4f\u89c8\u5668\u5730\u5740\u680f\u663e\u793a\u7684\u65b0\u7684 URL\u3002\u6b64 URL \u5fc5\u987b\u4e0e\u5f53\u524d\u9875\u9762\u7684\u57df\u3001\u534f\u8bae\u548c\u7aef\u53e3\u76f8\u540c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  history.pushState({ page: 1 }, 'Page 1', '\/page1');<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><code>replaceState(state, title, url)<\/code><\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0e <code>pushState()<\/code> \u76f8\u4f3c\uff0c\u4f46\u5b83\u66ff\u6362\u5f53\u524d\u5386\u53f2\u8bb0\u5f55\u6761\u76ee\uff0c\u800c\u4e0d\u662f\u6dfb\u52a0\u65b0\u6761\u76ee\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  history.replaceState({ page: 2 }, 'Page 2', '\/page2');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4e0e <code>popstate<\/code> \u4e8b\u4ef6\u7ed3\u5408\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f7f\u7528 <code>pushState<\/code> \u6216 <code>replaceState<\/code> \u4fee\u6539\u6d4f\u89c8\u5668\u5386\u53f2\u8bb0\u5f55\u65f6\uff0c\u6d4f\u89c8\u5668\u4f1a\u89e6\u53d1 <code>popstate<\/code> \u4e8b\u4ef6\u3002\u8fd9\u4e2a\u4e8b\u4ef6\u4f1a\u5728\u5386\u53f2\u8bb0\u5f55\u6761\u76ee\u53d8\u5316\u65f6\u89e6\u53d1\uff0c\u5305\u62ec\u6d4f\u89c8\u5668\u7684\u540e\u9000\u548c\u524d\u8fdb\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u76d1\u542c history \u72b6\u6001\u53d8\u5316\nwindow.addEventListener('popstate', function (event) {\n  console.log('State changed:', event.state);\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u8c03\u7528 <code>pushState<\/code> \u6216 <code>replaceState<\/code> \u65f6\uff0c\u4e8b\u4ef6\u7684 <code>state<\/code> \u5c5e\u6027\u5305\u542b\u4e86\u4f60\u4f20\u9012\u7ed9\u5b83\u7684\u72b6\u6001\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>history.pushState({ page: 1 }, 'Page 1', '\/page1');\n\/\/ \u7136\u540e\u7528\u6237\u70b9\u51fb\u540e\u9000\nwindow.addEventListener('popstate', function (event) {\n  console.log('State:', event.state);  \/\/ \u8f93\u51fa\uff1a{ page: 1 }\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong><code>history<\/code> \u5bf9\u8c61\u7684\u5b9e\u6218\u6848\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6848\u4f8b 1: \u5355\u9875\u5e94\u7528\u4e2d\u7684\u9875\u9762\u5bfc\u822a<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5355\u9875\u5e94\u7528\uff08SPA\uff09\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528 <code>pushState<\/code> \u6765\u6539\u53d8 URL\uff0c\u800c\u4e0d\u5237\u65b0\u9875\u9762\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>pushState<\/code> \u6765\u7ba1\u7406\u4e0d\u540c\u7684\u9875\u9762\u72b6\u6001\u5e76\u4fdd\u6301\u6d4f\u89c8\u5386\u53f2\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6a21\u62df\u4e00\u4e2a\u7b80\u5355\u7684\u9875\u9762\u5bfc\u822a\ndocument.getElementById('page1').addEventListener('click', function() {\n  history.pushState({ page: 1 }, 'Page 1', '\/page1');\n  document.getElementById('content').innerHTML = 'Page 1 Content';\n});\n\ndocument.getElementById('page2').addEventListener('click', function() {\n  history.pushState({ page: 2 }, 'Page 2', '\/page2');\n  document.getElementById('content').innerHTML = 'Page 2 Content';\n});<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6848\u4f8b 2: \u624b\u52a8\u63a7\u5236\u6d4f\u89c8\u5668\u5386\u53f2<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u5728\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u4e2d\u524d\u8fdb\u6216\u540e\u9000\u4e00\u9875\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>history.go()<\/code> \u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u624b\u52a8\u540e\u9000\u4e00\u9875\ndocument.getElementById('backBtn').addEventListener('click', function() {\n  window.history.go(-1);  \/\/ \u540e\u9000\u4e00\u9875\n});\n\n\/\/ \u624b\u52a8\u524d\u8fdb\u4e00\u9875\ndocument.getElementById('forwardBtn').addEventListener('click', function() {\n  window.history.go(1);   \/\/ \u524d\u8fdb\u4e00\u9875\n});<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6848\u4f8b 3: \u4fee\u6539 URL \u800c\u4e0d\u5237\u65b0\u9875\u9762<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>replaceState<\/code> \u6765\u66f4\u6539 URL\uff0c\u540c\u65f6\u4fdd\u6301\u5f53\u524d\u9875\u9762\u5185\u5bb9\u4e0d\u53d8\u3002\u6bd4\u5982\uff0c\u4f60\u7684\u5355\u9875\u5e94\u7528\u9700\u8981\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u4fee\u6539 URL\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5728\u7528\u6237\u70b9\u51fb\u67d0\u4e2a\u6309\u94ae\u65f6\u66f4\u65b0 URL\uff0c\u4f46\u4e0d\u5237\u65b0\u9875\u9762\ndocument.getElementById('changePage').addEventListener('click', function() {\n  history.replaceState({ page: 'new' }, 'New Page', '\/new');\n  document.getElementById('content').innerHTML = 'New Page Content';\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>window.history<\/code> \u5bf9\u8c61\u662f JavaScript \u4e2d\u7528\u4e8e\u64cd\u4f5c\u6d4f\u89c8\u5668\u5386\u53f2\u8bb0\u5f55\u7684\u91cd\u8981\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u4f60\u8bbf\u95ee\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u5806\u6808\uff0c\u5e76\u63d0\u4f9b\u4e86\u524d\u8fdb\u3001\u540e\u9000\u3001\u5237\u65b0\u3001\u4fee\u6539 URL \u7b49\u591a\u79cd\u529f\u80fd\u3002\u901a\u8fc7 <code>pushState<\/code> \u548c <code>replaceState<\/code> \u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u6d4f\u89c8\u5668\u5730\u5740\u680f\u7684 URL\uff0c\u8fd9\u662f\u5355\u9875\u5e94\u7528\uff08SPA\uff09\u5f00\u53d1\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u529f\u80fd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u638c\u63e1 <code>window.history<\/code> \u5bf9\u8c61\u7684\u64cd\u4f5c\uff0c\u80fd\u5e2e\u52a9\u4f60\u5b9e\u73b0\u66f4\u4e30\u5bcc\u7684\u7528\u6237\u4ea4\u4e92\u548c\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u52a8\u6001\u6539\u53d8\u9875\u9762\u5185\u5bb9\u800c\u4e0d\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\u65f6\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>window.history \u5bf9\u8c61\u662f\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u7ba1\u7406\u63a5\u53e3\uff0c\u5b83\u5141\u8bb8\u4f60\u64cd\u4f5c\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u5806\u6808\uff0c\u5305\u62ec\u524d\u8fdb\u3001\u540e\u9000 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2147","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2147"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2147\/revisions"}],"predecessor-version":[{"id":2148,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2147\/revisions\/2148"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}