{"id":299520,"date":"2024-05-20T17:19:25","date_gmt":"2024-05-20T09:19:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/299520.html"},"modified":"2024-05-20T17:19:47","modified_gmt":"2024-05-20T09:19:47","slug":"js-%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e6%8b%a6%e6%88%aa-pushstate-%e4%ba%8b%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/299520.html","title":{"rendered":"JS \u7a0b\u5e8f\u5982\u4f55\u62e6\u622a pushState \u4e8b\u4ef6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27151634\/c20c306f-7cb7-443f-b936-8d1ac5748f67.webp\" alt=\"JS \u7a0b\u5e8f\u5982\u4f55\u62e6\u622a pushState \u4e8b\u4ef6\" \/><\/p>\n<p><p>JavaScript \u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u6216\u4fee\u6539 <code>pushState<\/code> \u65b9\u6cd5\u7684\u884c\u4e3a\u6765\u62e6\u622a <code>pushState<\/code> \u4e8b\u4ef6\uff0c<strong>\u8fd9\u662f\u5b9e\u73b0\u5355\u9875\u9762\u5e94\u7528\uff08SPA\uff09\u4e2d\u9875\u9762\u5207\u6362\u7684\u6838\u5fc3\u6280\u672f<\/strong>\u3002\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec <strong>\u4fee\u6539\u539f\u751f <code>pushState<\/code> \u65b9\u6cd5\u3001\u76d1\u542c <code>popstate<\/code> \u4e8b\u4ef6\u3001\u7ed3\u5408 <code>hashchange<\/code> \u4e8b\u4ef6\u4f7f\u7528<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u4e0d\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\u7684\u60c5\u51b5\u4e0b\uff0c\u6539\u53d8\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u548c\u5730\u5740\u680fURL\uff0c\u8fdb\u800c\u80fd\u591f\u66f4\u7cbe\u7ec6\u5730\u63a7\u5236SPA\u7684\u9875\u9762\u5237\u65b0\u548c\u72b6\u6001\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u6211\u4eec <strong>\u8be6\u7ec6\u5c55\u5f00<\/strong> \u5bf9 <strong>\u4fee\u6539\u539f\u751f <code>pushState<\/code> \u65b9\u6cd5<\/strong> \u7684\u4ecb\u7ecd\uff1a\u901a\u8fc7\u5bf9 <code>history<\/code> \u5bf9\u8c61\u7684 <code>pushState<\/code> \u65b9\u6cd5\u8fdb\u884c\u8986\u5199\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u63d2\u5165\u81ea\u5df1\u7684\u903b\u8f91\uff0c\u6bd4\u5982\u5728\u5730\u5740\u680fURL\u53d8\u5316\u524d\u540e\u8fdb\u884c\u64cd\u4f5c\uff0c\u6216\u5728URL\u6539\u53d8\u65f6\u53d1\u9001\u6570\u636e\u5230\u670d\u52a1\u5668\u3002\u8fd9\u79cd\u6280\u672f\u4f7f\u5f97SPA\u53ef\u4ee5\u5728\u7528\u6237\u6d4f\u89c8\u9875\u9762\u65f6\uff0c\u4e0d\u4ec5\u66f4\u65b0\u5185\u5bb9\u800c\u4e0d\u5237\u65b0\u9875\u9762\uff0c\u8fd8\u80fd\u591f\u4fdd\u6301\u6d4f\u89c8\u5668\u524d\u8fdb\u548c\u540e\u9000\u529f\u80fd\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4fee\u6539\u539f\u751fPUSHSTATE\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4e3a\u4e86\u62e6\u622a\u5e76\u589e\u5f3a <code>pushState<\/code> \u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u9700\u8981\u4fdd\u5b58\u539f\u6709\u7684 <code>pushState<\/code> \u51fd\u6570\u7684\u5f15\u7528\u3002\u7136\u540e\uff0c\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u6765\u8986\u76d6\u539f\u751f\u7684 <code>pushState<\/code> \u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u65b0\u51fd\u6570\u4e2d\uff0c\u9664\u4e86\u6267\u884c\u539f\u6709\u7684 <code>pushState<\/code> \u529f\u80fd\uff0c\u5f00\u53d1\u8005\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u903b\u8f91\uff0c\u5982\u89e6\u53d1\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code>(function(history){<\/p>\n<p>    var pushState = history.pushState;<\/p>\n<p>    history.pushState = function(state, title, url) {<\/p>\n<p>        if (typeof history.onpushstate == &quot;function&quot;) {<\/p>\n<p>            history.onpushstate({state: state});<\/p>\n<p>        }<\/p>\n<p>        \/\/ \u8c03\u7528\u539f\u751f\u7684 pushState \u65b9\u6cd5<\/p>\n<p>        return pushState.apply(history, arguments);<\/p>\n<p>    }<\/p>\n<p>})(window.history);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u95ed\u5305\u5c06 <code>history<\/code> \u5bf9\u8c61\u7684\u539f\u751f <code>pushState<\/code> \u65b9\u6cd5\u5305\u88c5\u8d77\u6765\uff0c\u540c\u65f6\u5141\u8bb8\u5728\u8c03\u7528 <code>pushState<\/code> \u65b9\u6cd5\u65f6\u63d2\u5165\u81ea\u5b9a\u4e49\u7684\u903b\u8f91\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u4e0d\u4f1a\u7834\u574f\u539f\u6765 <code>pushState<\/code> \u65b9\u6cd5\u7684\u6b63\u5e38\u529f\u80fd\uff0c\u540c\u65f6\u53ef\u4ee5\u5728URL\u53d8\u5316\u65f6\u6267\u884c\u5f00\u53d1\u8005\u5b9a\u4e49\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u76d1\u542cPOPSTATE\u4e8b\u4ef6<\/h3>\n<\/p>\n<p><p>\u5f53\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u53d1\u751f\u53d8\u5316\u65f6\uff08\u5373\u7528\u6237\u70b9\u51fb\u524d\u8fdb\u6216\u540e\u9000\u6309\u94ae\uff09\uff0c<code>popstate<\/code> \u4e8b\u4ef6\u4f1a\u88ab\u89e6\u53d1\u3002\u901a\u8fc7\u76d1\u542c <code>popstate<\/code> \u4e8b\u4ef6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u5730\u5740\u680fURL\u53d8\u5316\u540e\u6267\u884c\u7279\u5b9a\u7684JavaScript\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code>window.addEventListener(&quot;popstate&quot;, function(event) {<\/p>\n<p>    console.log(&quot;location: &quot; + document.location + &quot;, state: &quot; + JSON.stringify(event.state));<\/p>\n<p>    \/\/ \u8fd9\u91cc\u53ef\u4ee5\u6839\u636e event.state \u6216\u5f53\u524d\u7684 URL \u6267\u884c\u76f8\u5e94\u903b\u8f91<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u76d1\u542c <code>popstate<\/code> \u4e8b\u4ef6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5bf9\u6d4f\u89c8\u5668\u5386\u53f2\u7684\u53d8\u5316\u4f5c\u51fa\u54cd\u5e94\uff0c\u6bd4\u5982\u52a0\u8f7d\u76f8\u5e94\u9875\u9762\u7684\u5185\u5bb9\uff0c\u8fd9\u5bf9\u4e8eSPA\u7684\u8def\u7531\u7ba1\u7406\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408HASHCHANGE\u4e8b\u4ef6\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u65e7\u7684\u6d4f\u89c8\u5668\u6216\u7279\u6b8a\u7684\u4f7f\u7528\u573a\u666f\uff0c\u53ef\u80fd\u9700\u8981\u7528\u5230 <code>hashchange<\/code> \u4e8b\u4ef6\u6765\u76d1\u542cURL\u4e2d\u54c8\u5e0c\uff08#\u540e\u7684\u90e8\u5206\uff09\u7684\u53d8\u5316\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u4e0e <code>pushState<\/code> \u548c <code>popstate<\/code> \u4e8b\u4ef6\u76f8\u6bd4\u8f83\u8d77\u6765\u66f4\u4e3a\u53e4\u8001\u548c\u5c40\u9650\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4ecd\u7136\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code>window.addEventListener(&quot;hashchange&quot;, function(){<\/p>\n<p>    console.log(&quot;Current hash:&quot;, window.location.hash);<\/p>\n<p>    \/\/ \u6839\u636e\u65b0\u7684 hash \u663e\u793a\u76f8\u5e94\u7684\u9875\u9762\u5185\u5bb9<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u76d1\u542c <code>hashchange<\/code> \u4e8b\u4ef6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728URL\u7684\u54c8\u5e0c\u90e8\u5206\u53d8\u5316\u65f6\u6267\u884c\u76f8\u5173\u903b\u8f91\uff0c\u8fd9\u901a\u5e38\u7528\u4e8e\u65e7\u5f0f\u7684 \u201chashbang\u201d \u8def\u7531\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u4e0e\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u5b9e\u73b0SPA\u8def\u7531\u7ba1\u7406\u662f\u4e0a\u8ff0\u6280\u672f\u6700\u5e38\u89c1\u7684\u7528\u9014\u3002\u5728\u5355\u9875\u9762\u5e94\u7528\u4e2d\uff0c\u5f00\u53d1\u8005\u9700\u8981\u6839\u636e\u4e0d\u540c\u7684URL\u663e\u793a\u4e0d\u540c\u7684\u5185\u5bb9\uff0c\u540c\u65f6\u53c8\u4e0d\u5237\u65b0\u9875\u9762\u3002\u901a\u8fc7\u62e6\u622a <code>pushState<\/code> \u4e8b\u4ef6\uff0c\u5e76\u7ed3\u5408 <code>popstate<\/code> \u548c <code>hashchange<\/code> \u4e8b\u4ef6\uff0c\u53ef\u4ee5\u975e\u5e38\u7075\u6d3b\u5730\u63a7\u5236\u9875\u9762\u5185\u5bb9\u7684\u663e\u793a\uff0c\u540c\u65f6\u4fdd\u6301\u5730\u5740\u680fURL\u7684\u53d8\u5316\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u76f4\u89c2\u7684\u6d4f\u89c8\u5386\u53f2\u548c\u4e66\u7b7e\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4e00\u4e2aSPA\u53ef\u80fd\u4f1a\u6839\u636e\u4e0d\u540c\u7684URL\u8def\u5f84\u52a0\u8f7d\u4e0d\u540c\u7684\u89c6\u56fe\u7ec4\u4ef6\uff0c\u800c\u901a\u8fc7\u62e6\u622a <code>pushState<\/code> \uff0c\u53ef\u4ee5\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u9875\u9762\u8bbf\u95ee\u7edf\u8ba1\u3001\u52a8\u6001\u6807\u9898\u66f4\u65b0\u7b49\u81ea\u5b9a\u4e49\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cJavaScript\u7a0b\u5e8f\u4e0d\u4ec5\u80fd\u591f\u62e6\u622a <code>pushState<\/code> \u4e8b\u4ef6\uff0c\u8fd8\u80fd\u63d0\u4f9b\u4e30\u5bcc\u7684SPA\u9875\u9762\u5bfc\u822a\u548c\u72b6\u6001\u7ba1\u7406\u80fd\u529b\uff0c\u4ece\u800c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662f pushState \u4e8b\u4ef6\uff0c\u5982\u4f55\u62e6\u622a\u5b83\uff1f<\/strong><\/p>\n<p>pushState \u4e8b\u4ef6\u662f HTML5 \u4e2d History API \u63d0\u4f9b\u7684\u4e00\u79cd\u64cd\u4f5c\u6d4f\u89c8\u5668\u5386\u53f2\u8bb0\u5f55\u7684\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u6d4f\u89c8\u5668\u5730\u5740\u680f\u4e2d\u7684 URL\uff0c\u5e76\u5c06\u76f8\u5e94\u7684\u5386\u53f2\u8bb0\u5f55\u6dfb\u52a0\u5230\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u6808\u4e2d\u3002<\/p>\n<p>\u8981\u62e6\u622a pushState \u4e8b\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 window.history.pushState \u65b9\u6cd5\u7684\u5c01\u88c5\uff0c\u901a\u8fc7\u91cd\u5199\u8be5\u65b9\u6cd5\u6765\u8fbe\u5230\u62e6\u622a\u76ee\u7684\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u539f\u59cb\u7684 pushState \u65b9\u6cd5\u4fdd\u5b58\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u91cd\u65b0\u5b9a\u4e49 pushState \u65b9\u6cd5\uff0c\u6dfb\u52a0\u9700\u8981\u6267\u884c\u7684\u989d\u5916\u4ee3\u7801\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728\u62e6\u622a pushState \u4e8b\u4ef6\u540e\u8fdb\u884c\u81ea\u5b9a\u4e49\u5904\u7406\uff1f<\/strong><\/p>\n<p>\u62e6\u622a pushState \u4e8b\u4ef6\u540e\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u81ea\u5b9a\u4e49\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u6bcf\u6b21\u62e6\u622a pushState \u4e8b\u4ef6\u65f6\uff0c\u8bb0\u5f55\u76f8\u5173\u4fe1\u606f\u5230\u65e5\u5fd7\u4e2d\uff0c\u4ece\u800c\u65b9\u4fbf\u8fdb\u884c\u8c03\u8bd5\u548c\u8ddf\u8e2a\u3002\u53e6\u5916\uff0c\u8fd8\u53ef\u4ee5\u6267\u884c\u4e00\u4e9b\u5176\u4ed6\u64cd\u4f5c\uff0c\u6bd4\u5982\u5728\u9875\u9762\u5730\u5740\u53d1\u751f\u6539\u53d8\u65f6\u66f4\u65b0\u9875\u9762\u5185\u5bb9\uff0c\u6216\u8005\u6267\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n<p><strong>3. \u62e6\u622a pushState \u4e8b\u4ef6\u6709\u4ec0\u4e48\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><\/p>\n<p>\u5728\u62e6\u622a pushState \u4e8b\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u4fdd\u8bc1\u62e6\u622a\u903b\u8f91\u7684\u7a33\u5b9a\u6027\uff1a\u8981\u786e\u4fdd\u91cd\u5199 pushState \u65b9\u6cd5\u540e\uff0c\u4e0d\u4f1a\u5bf9\u9875\u9762\u7684\u6b63\u5e38\u529f\u80fd\u9020\u6210\u5f71\u54cd\u3002<\/li>\n<li>\u5b58\u5728\u517c\u5bb9\u6027\u95ee\u9898\uff1a\u4e0d\u540c\u6d4f\u89c8\u5668\u5bf9 pushState \u4e8b\u4ef6\u7684\u5904\u7406\u65b9\u5f0f\u53ef\u80fd\u5b58\u5728\u5dee\u5f02\uff0c\u9700\u8981\u5bf9\u4e0d\u540c\u6d4f\u89c8\u5668\u8fdb\u884c\u9002\u914d\u3002<\/li>\n<li>\u4e0d\u6ee5\u7528\u62e6\u622a\uff1a\u62e6\u622a pushState \u4e8b\u4ef6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u4e0d\u53ef\u9884\u671f\u7684\u95ee\u9898\uff0c\u56e0\u6b64\u5e94\u8be5\u614e\u91cd\u4f7f\u7528\uff0c\u5e76\u786e\u4fdd\u53ea\u5728\u9700\u8981\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u62e6\u622a\u3002<\/li>\n<li>\u6d4b\u8bd5\u548c\u8c03\u8bd5\uff1a\u5728\u62e6\u622a pushState \u4e8b\u4ef6\u524d\uff0c\u5e94\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5\u548c\u8c03\u8bd5\uff0c\u4ee5\u786e\u4fdd\u62e6\u622a\u903b\u8f91\u7684\u6b63\u786e\u6027\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"JavaScript \u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u6216\u4fee\u6539 pushState \u65b9\u6cd5\u7684\u884c\u4e3a\u6765\u62e6\u622a pushState \u4e8b\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":299548,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/299520"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=299520"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/299520\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/299548"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=299520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=299520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=299520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}