{"id":856,"date":"2025-12-22T09:03:15","date_gmt":"2025-12-22T01:03:15","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=856"},"modified":"2025-12-22T09:03:15","modified_gmt":"2025-12-22T01:03:15","slug":"js%e6%8e%a7%e5%88%b6%e6%b5%8f%e8%a7%88%e5%99%a8%e5%89%8d%e8%bf%9b%e3%80%81%e5%90%8e%e9%80%80%e3%80%81%e9%a1%b5%e9%9d%a2%e8%b7%b3%e8%bd%ac%e8%af%a6%e7%bb%86%e6%96%b9%e6%b3%95%e5%92%8c%e7%a4%ba-2","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/22\/js%e6%8e%a7%e5%88%b6%e6%b5%8f%e8%a7%88%e5%99%a8%e5%89%8d%e8%bf%9b%e3%80%81%e5%90%8e%e9%80%80%e3%80%81%e9%a1%b5%e9%9d%a2%e8%b7%b3%e8%bd%ac%e8%af%a6%e7%bb%86%e6%96%b9%e6%b3%95%e5%92%8c%e7%a4%ba-2\/","title":{"rendered":"js\u63a7\u5236\u6d4f\u89c8\u5668\u524d\u8fdb\u3001\u540e\u9000\u3001\u9875\u9762\u8df3\u8f6c\u8be6\u7ec6\u65b9\u6cd5\u548c\u793a\u4f8b"},"content":{"rendered":"\n<p>JavaScript \u63a7\u5236\u6d4f\u89c8\u5668\u524d\u8fdb\u3001\u540e\u9000\u3001\u9875\u9762\u8df3\u8f6c\u7684\u8be6\u7ec6\u65b9\u6cd5\u548c\u793a\u4f8b\u5728 JavaScript \u4e2d\uff0c\u63a7\u5236\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u3001\u540e\u9000\u3001\u5237\u65b0\u548c\u9875\u9762\u8df3\u8f6c\u4e3b\u8981\u901a\u8fc7 window.history \u548c location \u5bf9\u8c61\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f 2025 \u5e74\u6700\u5e38\u7528\u3001\u6700\u89c4\u8303\u7684\u65b9\u6cd5\u548c\u793a\u4f8b\u30021. \u540e\u9000\uff08Back\uff09 \/ \u524d\u8fdb\uff08Forward\uff09<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u64cd\u4f5c<\/th><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><\/tr><tr><td>\u540e\u9000\u4e00\u6b65<\/td><td>history.back()<\/td><td>\u7b49\u4ef7\u4e8e\u6d4f\u89c8\u5668\u5de5\u5177\u680f\u7684\u201c\u2190\u201d\u6309\u94ae<\/td><\/tr><tr><td>\u524d\u8fdb\u4e00\u6b65<\/td><td>history.forward()<\/td><td>\u7b49\u4ef7\u4e8e\u6d4f\u89c8\u5668\u5de5\u5177\u680f\u7684\u201c\u2192\u201d\u6309\u94ae<\/td><\/tr><tr><td>\u540e\u9000\/\u524d\u8fdb N \u6b65<\/td><td>history.go(n)<\/td><td>n &gt; 0 \u524d\u8fdb\uff0cn &lt; 0 \u540e\u9000\uff0cn = 0 \u5237\u65b0\u5f53\u524d\u9875<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>html<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;button onclick=&quot;history.back()&quot;&gt;\u2190 \u8fd4\u56de\u4e0a\u4e00\u9875&amp;lt;\/button&gt;\n&amp;lt;button onclick=&quot;history.forward()&quot;&gt;\u2192 \u524d\u8fdb\u5230\u4e0b\u4e00\u9875&amp;lt;\/button&gt;\n&amp;lt;button onclick=&quot;history.go(-2)&quot;&gt;\u540e\u9000\u4e24\u9875&amp;lt;\/button&gt;\n&amp;lt;button onclick=&quot;history.go(1)&quot;&gt;\u524d\u8fdb\u4e00\u9875&amp;lt;\/button&gt;\n&amp;lt;button onclick=&quot;history.go(0)&quot;&gt;\u5237\u65b0\u5f53\u524d\u9875&amp;lt;\/button&gt;\n<\/pre><\/div>\n\n\n<p>2. \u9875\u9762\u8df3\u8f6c\uff08Redirect\uff09<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><th>\u662f\u5426\u52a0\u5165\u5386\u53f2\u8bb0\u5f55<\/th><th>\u63a8\u8350\u573a\u666f<\/th><\/tr><tr><td>location.href = url<\/td><td>\u6700\u5e38\u7528\uff0c\u7acb\u5373\u8df3\u8f6c<\/td><td>\u662f<\/td><td>\u666e\u901a\u8df3\u8f6c<\/td><\/tr><tr><td>location.assign(url)<\/td><td>\u529f\u80fd\u4e0e location.href \u76f8\u540c\uff0c\u4f46\u66f4\u89c4\u8303\u3001\u53ef\u88ab\u5355\u5143\u6d4b\u8bd5\u62e6\u622a<\/td><td>\u662f<\/td><td>\u63a8\u8350\u5199\u6cd5<\/td><\/tr><tr><td>location.replace(url)<\/td><td>\u8df3\u8f6c\u4f46\u4e0d\u8bb0\u5f55\u5386\u53f2\uff0c\u7528\u6237\u65e0\u6cd5\u201c\u540e\u9000\u201d\u8fd4\u56de<\/td><td>\u5426<\/td><td>\u767b\u5f55\u6210\u529f\u540e\u8df3\u8f6c\u3001404 \u91cd\u5b9a\u5411<\/td><\/tr><tr><td>location = url<\/td><td>\u7b49\u4ef7\u4e8e location.href = url<\/td><td>\u662f<\/td><td>\u7b80\u5199\uff08\u4e0d\u63a8\u8350\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>javascript<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u666e\u901a\u8df3\u8f6c\uff08\u52a0\u5165\u5386\u53f2\u8bb0\u5f55\uff09\nfunction gotoBaidu() {\n  location.href = &quot;https:\/\/www.baidu.com&quot;;\n  \/\/ \u6216 location.assign(&quot;https:\/\/www.baidu.com&quot;);\n}\n\n\/\/ \u66ff\u6362\u5f0f\u8df3\u8f6c\uff08\u4e0d\u8bb0\u5f55\u5386\u53f2\uff09\nfunction loginSuccess() {\n  location.replace(&quot;\/dashboard&quot;);\n}\n\n\/\/ \u76f8\u5bf9\u8def\u5f84\u8df3\u8f6c\nlocation.href = &quot;\/user\/profile&quot;;    \/\/ \u8df3\u8f6c\u5230\u5f53\u524d\u57df\u540d\u7684 \/user\/profile\nlocation.href = &quot;..\/login.html&quot;;    \/\/ \u76f8\u5bf9\u8def\u5f84\nlocation.href = &quot;#top&quot;;             \/\/ \u8df3\u8f6c\u5230\u9875\u9762\u951a\u70b9\uff08\u4e0d\u5237\u65b0\uff09\n<\/pre><\/div>\n\n\n<p>3. \u5237\u65b0\u9875\u9762<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><\/tr><tr><td>location.reload()<\/td><td>\u5237\u65b0\u5f53\u524d\u9875\u9762\uff08\u9ed8\u8ba4\u4ece\u6d4f\u89c8\u5668\u7f13\u5b58\u52a0\u8f7d\uff09<\/td><\/tr><tr><td>location.reload(true)<\/td><td>\u5f3a\u5236\u4ece\u670d\u52a1\u5668\u91cd\u65b0\u52a0\u8f7d\uff08\u5ffd\u7565\u7f13\u5b58\uff09<\/td><\/tr><tr><td>history.go(0)<\/td><td>\u7b49\u4ef7\u4e8e location.reload()<\/td><\/tr><tr><td>location.href = location.href<\/td><td>\u5f3a\u5236\u5237\u65b0\uff08\u6700\u7b80\u5355\u4e14\u517c\u5bb9\u6027\u597d\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>javascript<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u666e\u901a\u5237\u65b0\uff08\u53ef\u80fd\u7528\u7f13\u5b58\uff09\nlocation.reload();\n\n\/\/ \u5f3a\u5236\u5237\u65b0\uff08\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6700\u65b0\u7248\u672c\uff09\nlocation.reload(true);  \/\/ \u6ce8\u610f\uff1atrue \u53c2\u6570\u5728\u90e8\u5206\u73b0\u4ee3\u6d4f\u89c8\u5668\u5df2\u5ffd\u7565\uff0c\u4f46\u4ecd\u517c\u5bb9\n\n\/\/ \u63a8\u8350\u5199\u6cd5\uff08\u6700\u53ef\u9760\uff09\nlocation.href = location.href;\n<\/pre><\/div>\n\n\n<p>4. \u5b8c\u6574\u793a\u4f8b\uff1a\u5bfc\u822a\u680f + \u8df3\u8f6c\u63a7\u5236<\/p>\n\n\n\n<p>html<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;!DOCTYPE html&gt;\n&amp;lt;html lang=&quot;zh-CN&quot;&gt;\n&amp;lt;head&gt;\n  &amp;lt;meta charset=&quot;UTF-8&quot;&gt;\n  &amp;lt;title&gt;JS \u5bfc\u822a\u63a7\u5236&amp;lt;\/title&gt;\n  &amp;lt;style&gt;\n    button { margin: 5px; padding: 8px 16px; }\n  &amp;lt;\/style&gt;\n&amp;lt;\/head&gt;\n&amp;lt;body&gt;\n  &amp;lt;h2&gt;\u6d4f\u89c8\u5668\u5bfc\u822a\u63a7\u5236&amp;lt;\/h2&gt;\n  \n  &amp;lt;button onclick=&quot;history.back()&quot;&gt;\u2190 \u540e\u9000&amp;lt;\/button&gt;\n  &amp;lt;button onclick=&quot;history.forward()&quot;&gt;\u2192 \u524d\u8fdb&amp;lt;\/button&gt;\n  &amp;lt;button onclick=&quot;history.go(0)&quot;&gt;\u5237\u65b0&amp;lt;\/button&gt;\n  \n  &amp;lt;hr&gt;\n  \n  &amp;lt;button onclick=&quot;gotoPage(&#039;https:\/\/www.google.com&#039;)&quot;&gt;\u8df3\u8f6c Google&amp;lt;\/button&gt;\n  &amp;lt;button onclick=&quot;replacePage(&#039;\/dashboard&#039;)&quot;&gt;\u66ff\u6362\u8df3\u8f6c\uff08\u65e0\u5386\u53f2\uff09&amp;lt;\/button&gt;\n  &amp;lt;button onclick=&quot;openNewTab(&#039;https:\/\/github.com&#039;)&quot;&gt;\u65b0\u6807\u7b7e\u9875\u6253\u5f00&amp;lt;\/button&gt;\n\n  &amp;lt;script&gt;\n    function gotoPage(url) {\n      location.assign(url); \/\/ \u6216 location.href = url\n    }\n\n    function replacePage(url) {\n      location.replace(url);\n    }\n\n    function openNewTab(url) {\n      window.open(url, &#039;_blank&#039;); \/\/ \u65b0\u6807\u7b7e\u9875\n    }\n  &amp;lt;\/script&gt;\n&amp;lt;\/body&gt;\n&amp;lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p>5. \u9ad8\u7ea7\u7528\u6cd5\uff1a\u76d1\u542c\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\/\u540e\u9000<\/p>\n\n\n\n<p>javascript<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u76d1\u542c\u7528\u6237\u70b9\u51fb\u6d4f\u89c8\u5668\u524d\u8fdb\/\u540e\u9000\u6309\u94ae\uff08popstate \u4e8b\u4ef6\uff09\nwindow.addEventListener(&#039;popstate&#039;, function(event) {\n  console.log(&#039;\u7528\u6237\u89e6\u53d1\u4e86\u524d\u8fdb\u6216\u540e\u9000\uff01\u5f53\u524d URL:&#039;, location.href);\n  \/\/ \u53ef\u5728\u6b64\u66f4\u65b0\u9875\u9762\u5185\u5bb9\uff08\u5b9e\u73b0\u5355\u9875\u5e94\u7528 SPA \u8def\u7531\uff09\n});\n\n\/\/ \u624b\u52a8\u6dfb\u52a0\u5386\u53f2\u8bb0\u5f55\uff08\u5e38\u7528\u4e8e SPA\uff09\nhistory.pushState({ page: 1 }, &quot;\u6807\u9898&quot;, &quot;?page=1&quot;);\n\/\/ \u66ff\u6362\u5f53\u524d\u5386\u53f2\u8bb0\u5f55\nhistory.replaceState({ page: 2 }, &quot;\u65b0\u6807\u9898&quot;, &quot;?page=2&quot;);\n<\/pre><\/div>\n\n\n<p>6. \u6ce8\u610f\u4e8b\u9879 &amp; \u5e38\u89c1\u95ee\u9898<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u95ee\u9898<\/th><th>\u539f\u56e0\/\u8bf4\u660e<\/th><th>\u89e3\u51b3\u65b9\u6848<\/th><\/tr><tr><td>history.back() \u4e0d\u751f\u6548<\/td><td>\u5386\u53f2\u8bb0\u5f55\u4e2d\u6ca1\u6709\u4e0a\u4e00\u9875\uff08\u65b0\u5f00\u6807\u7b7e\u9875\uff09<\/td><td>\u68c0\u67e5 history.length<\/td><\/tr><tr><td>\u8df3\u8f6c\u540e URL \u5e26 #<\/td><td>\u4f7f\u7528\u4e86 href=&#8221;#&#8221;<\/td><td>\u6539\u7528 javascript:void(0) \u6216 preventDefault()<\/td><\/tr><tr><td>\u8de8\u57df\u9650\u5236<\/td><td>\u53ea\u80fd\u64cd\u4f5c\u540c\u6e90\u9875\u9762\u5386\u53f2\u8bb0\u5f55<\/td><td>\u2014<\/td><\/tr><tr><td>location.replace() \u540e\u65e0\u6cd5\u540e\u9000<\/td><td>\u8fd9\u662f\u5b83\u7684\u8bbe\u8ba1\u76ee\u7684<\/td><td>\u9700\u8981\u540e\u9000\u65f6\u7528 location.href<\/td><\/tr><tr><td>\u79fb\u52a8\u7aef\/iOS Safari \u95ee\u9898<\/td><td>history.back() \u6709\u65f6\u4e0d\u89e6\u53d1 popstate<\/td><td>\u989d\u5916\u76d1\u542c hashchange \u6216\u7528\u5e93<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>7. \u603b\u7ed3\u5bf9\u6bd4\u8868\uff082025 \u5e74\u63a8\u8350\u4f18\u5148\u7ea7\uff09<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u9700\u6c42<\/th><th>\u9996\u9009\u65b9\u6cd5<\/th><th>\u5907\u9009\u65b9\u6cd5<\/th><\/tr><tr><td>\u540e\u9000<\/td><td>history.back()<\/td><td>history.go(-1)<\/td><\/tr><tr><td>\u524d\u8fdb<\/td><td>history.forward()<\/td><td>history.go(1)<\/td><\/tr><tr><td>\u666e\u901a\u8df3\u8f6c\uff08\u4fdd\u7559\u5386\u53f2\uff09<\/td><td>location.assign(url)<\/td><td>location.href = url<\/td><\/tr><tr><td>\u66ff\u6362\u8df3\u8f6c\uff08\u65e0\u5386\u53f2\uff09<\/td><td>location.replace(url)<\/td><td>\u2014<\/td><\/tr><tr><td>\u5237\u65b0<\/td><td>location.reload()<\/td><td>location.href = location.href<\/td><\/tr><tr><td>\u65b0\u6807\u7b7e\u9875\u6253\u5f00<\/td><td>window.open(url, &#8216;_blank&#8217;)<\/td><td>\u2014<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4e00\u53e5\u8bdd\u8bb0\u4f4f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u60f3\u4fdd\u7559\u5386\u53f2 \u2192 \u7528 location.assign \/ href<\/li>\n\n\n\n<li>\u4e0d\u60f3\u7559\u4e0b\u5386\u53f2 \u2192 \u7528 location.replace<\/li>\n\n\n\n<li>\u6a21\u62df\u6d4f\u89c8\u5668\u6309\u94ae \u2192 \u7528 history.back() \/ forward() \/ go(n)<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u4f60\u7684\u9879\u76ee\u662f\u5355\u9875\u5e94\u7528\uff08SPA\uff09\uff0c\u63a8\u8350\u4f7f\u7528 history.pushState + popstate \u5b9e\u73b0\u8def\u7531\u3002\u5982\u679c\u4f60\u6709\u5177\u4f53\u573a\u666f\uff08\u6bd4\u5982 Vue\/React \u8def\u7531\u63a7\u5236\u3001\u5fae\u4fe1\u5185\u8df3\u8f6c\u7b49\uff09\uff0c\u6b22\u8fce\u8865\u5145\uff0c\u6211\u53ef\u4ee5\u7ed9\u51fa\u66f4\u9488\u5bf9\u6027\u7684\u4ee3\u7801\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u63a7\u5236\u6d4f\u89c8\u5668\u524d\u8fdb\u3001\u540e\u9000\u3001\u9875\u9762\u8df3\u8f6c\u7684\u8be6\u7ec6\u65b9\u6cd5\u548c\u793a\u4f8b\u5728 Java&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/22\/js%e6%8e%a7%e5%88%b6%e6%b5%8f%e8%a7%88%e5%99%a8%e5%89%8d%e8%bf%9b%e3%80%81%e5%90%8e%e9%80%80%e3%80%81%e9%a1%b5%e9%9d%a2%e8%b7%b3%e8%bd%ac%e8%af%a6%e7%bb%86%e6%96%b9%e6%b3%95%e5%92%8c%e7%a4%ba-2\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-856","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=856"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/856\/revisions"}],"predecessor-version":[{"id":857,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/856\/revisions\/857"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}