{"id":81,"date":"2025-11-20T17:06:19","date_gmt":"2025-11-20T09:06:19","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=81"},"modified":"2025-11-20T17:06:19","modified_gmt":"2025-11-20T09:06:19","slug":"window-location-href%e7%9a%84%e4%bb%8b%e7%bb%8d%e5%8f%8a%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/11\/20\/window-location-href%e7%9a%84%e4%bb%8b%e7%bb%8d%e5%8f%8a%e4%bd%bf%e7%94%a8\/","title":{"rendered":"window.location.href\u7684\u4ecb\u7ecd\u53ca\u4f7f\u7528"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong><code>window.location.href<\/code>&nbsp;\u7684\u4ecb\u7ecd\u4e0e\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<p><code>window.location.href<\/code>&nbsp;\u662f JavaScript \u4e2d&nbsp;<code>window.location<\/code>&nbsp;\u5bf9\u8c61\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u7528\u4e8e\u83b7\u53d6\u6216\u8bbe\u7f6e\u5f53\u524d\u7f51\u9875\u7684\u5b8c\u6574 URL\uff08\u7edf\u4e00\u8d44\u6e90\u5b9a\u4f4d\u7b26\uff09\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u83b7\u53d6\u5f53\u524d\u9875\u9762\u7684\u5730\u5740\uff0c\u5e76\u4e14\u4e5f\u53ef\u4ee5\u7528\u6765\u8df3\u8f6c\u5230\u5176\u4ed6\u7f51\u9875\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.&nbsp;<code>window.location<\/code>&nbsp;\u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<p><code>window.location<\/code>&nbsp;\u662f\u4e00\u4e2a\u5305\u542b\u5f53\u524d\u6587\u6863 URL \u76f8\u5173\u4fe1\u606f\u7684\u5bf9\u8c61\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u7528\u4e8e\u83b7\u53d6\u548c\u64cd\u4f5c\u6d4f\u89c8\u5668\u7684\u5730\u5740\u680f URL\u3002<\/p>\n\n\n\n<p><strong><code>window.location<\/code>&nbsp;\u5bf9\u8c61\u7684\u5e38\u7528\u5c5e\u6027<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>href<\/code><\/strong>: \u8fd4\u56de\u5f53\u524d\u6587\u6863\u7684\u5b8c\u6574 URL\uff0c\u53ef\u4ee5\u901a\u8fc7\u8be5\u5c5e\u6027\u8fdb\u884c\u9875\u9762\u8df3\u8f6c\u3002<\/li>\n\n\n\n<li><strong><code>protocol<\/code><\/strong>: \u8fd4\u56de\u5f53\u524d URL \u7684\u534f\u8bae\u90e8\u5206\uff08\u4f8b\u5982\uff0c<code>http:<\/code>\u00a0\u6216\u00a0<code>https:<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong><code>host<\/code><\/strong>: \u8fd4\u56de\u5f53\u524d URL \u7684\u4e3b\u673a\u540d\uff08\u5305\u62ec\u7aef\u53e3\u53f7\uff0c\u82e5\u6709\uff09\u3002<\/li>\n\n\n\n<li><strong><code>hostname<\/code><\/strong>: \u8fd4\u56de\u5f53\u524d URL \u7684\u4e3b\u673a\u540d\uff08\u4e0d\u5305\u62ec\u7aef\u53e3\u53f7\uff09\u3002<\/li>\n\n\n\n<li><strong><code>pathname<\/code><\/strong>: \u8fd4\u56de\u5f53\u524d URL \u4e2d\u7684\u8def\u5f84\u90e8\u5206\u3002<\/li>\n\n\n\n<li><strong><code>search<\/code><\/strong>: \u8fd4\u56de\u5f53\u524d URL \u4e2d\u67e5\u8be2\u5b57\u7b26\u4e32\u90e8\u5206\uff08\u4f8b\u5982\uff0c<code>?id=123<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong><code>hash<\/code><\/strong>: \u8fd4\u56de\u5f53\u524d URL \u4e2d\u7684\u951a\u70b9\u90e8\u5206\uff08\u4f8b\u5982\uff0c<code>#section1<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.&nbsp;<code>window.location.href<\/code><\/strong><\/h3>\n\n\n\n<p><code>window.location.href<\/code>&nbsp;\u662f&nbsp;<code>window.location<\/code>&nbsp;\u5bf9\u8c61\u7684\u4e00\u4e2a\u5b57\u7b26\u4e32\u5c5e\u6027\uff0c\u8868\u793a\u5f53\u524d\u7f51\u9875\u7684\u5b8c\u6574 URL\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bfb\u53d6\u00a0<code>href<\/code><\/strong>\uff1a\u8fd4\u56de\u5f53\u524d\u9875\u9762\u7684\u5b8c\u6574 URL\uff08\u5305\u62ec\u534f\u8bae\u3001\u57df\u540d\u3001\u8def\u5f84\u3001\u67e5\u8be2\u5b57\u7b26\u4e32\u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong>\u4fee\u6539\u00a0<code>href<\/code><\/strong>\uff1a\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684 URL\uff0c\u53ef\u4ee5\u7528\u6765\u8df3\u8f6c\u5230\u5176\u4ed6\u7f51\u9875\u6216\u91cd\u65b0\u52a0\u8f7d\u5f53\u524d\u9875\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.&nbsp;<code>window.location.href<\/code>&nbsp;\u7684\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1&nbsp;<strong>\u83b7\u53d6\u5f53\u524d\u9875\u9762\u7684 URL<\/strong><\/h4>\n\n\n\n<p>\u901a\u8fc7\u8bbf\u95ee&nbsp;<code>window.location.href<\/code>\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u9875\u9762\u7684\u5b8c\u6574 URL\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u83b7\u53d6\u5f53\u524d\u9875\u9762\u7684\u5b8c\u6574 URL\nlet currentUrl = window.location.href;\nconsole.log(currentUrl); \/\/ \u8f93\u51fa\u5f53\u524d\u9875\u9762\u7684 URL\n\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">3.2&nbsp;<strong>\u8df3\u8f6c\u5230\u53e6\u4e00\u4e2a URL<\/strong><\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e&nbsp;<code>window.location.href<\/code>&nbsp;\u4e3a\u65b0\u7684 URL \u6765\u5b9e\u73b0\u9875\u9762\u8df3\u8f6c\u3002\u5f53\u4f60\u8bbe\u7f6e&nbsp;<code>href<\/code>&nbsp;\u5c5e\u6027\u65f6\uff0c\u6d4f\u89c8\u5668\u4f1a\u52a0\u8f7d\u65b0\u7684\u9875\u9762\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u8df3\u8f6c\u5230\u53e6\u4e00\u4e2a\u9875\u9762\nwindow.location.href = &quot;https:\/\/www.example.com&quot;;\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u5c06\u4f7f\u6d4f\u89c8\u5668\u8df3\u8f6c\u5230&nbsp;<code>https:\/\/www.example.com<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.3&nbsp;<strong>\u91cd\u65b0\u52a0\u8f7d\u5f53\u524d\u9875\u9762<\/strong><\/h4>\n\n\n\n<p>\u901a\u8fc7\u5c06&nbsp;<code>window.location.href<\/code>&nbsp;\u8bbe\u7f6e\u4e3a\u5f53\u524d URL\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u9875\u9762\u7684\u91cd\u65b0\u52a0\u8f7d\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u91cd\u65b0\u52a0\u8f7d\u5f53\u524d\u9875\u9762\nwindow.location.href = window.location.href;\n\n<\/pre><\/div>\n\n\n<p>\u6216\u8005\u66f4\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u7528&nbsp;<code>window.location.reload()<\/code>&nbsp;\u65b9\u6cd5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u91cd\u65b0\u52a0\u8f7d\u5f53\u524d\u9875\u9762\nwindow.location.reload();\n\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">3.4&nbsp;<strong>\u901a\u8fc7\u67e5\u8be2\u5b57\u7b26\u4e32\u8fdb\u884c\u8df3\u8f6c<\/strong><\/h4>\n\n\n\n<p>\u4f60\u4e5f\u53ef\u4ee5\u52a8\u6001\u6784\u9020 URL\uff0c\u5e76\u8df3\u8f6c\u5230\u5305\u542b\u67e5\u8be2\u53c2\u6570\u7684\u9875\u9762\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u6839\u636e\u67d0\u4e2a\u6761\u4ef6\u8df3\u8f6c\u5230\u5e26\u6709\u67e5\u8be2\u5b57\u7b26\u4e32\u7684 URL\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u6784\u9020\u4e00\u4e2a\u5e26\u67e5\u8be2\u5b57\u7b26\u4e32\u7684 URL\nlet baseUrl = &quot;https:\/\/www.example.com\/search&quot;;\nlet queryParams = &quot;?query=JavaScript&quot;;\nlet newUrl = baseUrl + queryParams;\n\n\/\/ \u8df3\u8f6c\u5230\u5e26\u67e5\u8be2\u5b57\u7b26\u4e32\u7684 URL\nwindow.location.href = newUrl;\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u5c06\u4f7f\u6d4f\u89c8\u5668\u8df3\u8f6c\u5230&nbsp;<code>https:\/\/www.example.com\/search?query=JavaScript<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.&nbsp;<code>window.location.href<\/code>&nbsp;\u8df3\u8f6c\u4e0e&nbsp;<code>window.location.replace()<\/code><\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>window.location.href<\/code><\/strong>\u00a0\u4f1a\u5c06\u5f53\u524d\u9875\u9762\u63a8\u5165\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u6808\uff0c\u56e0\u6b64\u7528\u6237\u53ef\u4ee5\u70b9\u51fb\u6d4f\u89c8\u5668\u7684\u201c\u8fd4\u56de\u201d\u6309\u94ae\u8fd4\u56de\u5230\u524d\u4e00\u4e2a\u9875\u9762\u3002<\/li>\n\n\n\n<li><strong><code>window.location.replace()<\/code><\/strong>\u00a0\u5219\u66ff\u4ee3\u5f53\u524d\u9875\u9762\uff0c\u800c\u4e0d\u4f1a\u5c06\u5f53\u524d\u9875\u9762\u6dfb\u52a0\u5230\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u4e2d\uff0c\u56e0\u6b64\u7528\u6237\u4e0d\u80fd\u4f7f\u7528\u201c\u8fd4\u56de\u201d\u6309\u94ae\u8fd4\u56de\u5230\u5f53\u524d\u9875\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u8df3\u8f6c\u5230\u65b0\u9875\u9762\uff0c\u5141\u8bb8\u8fd4\u56de\nwindow.location.href = &quot;https:\/\/www.example.com&quot;;\n\n\/\/ \u66ff\u4ee3\u5f53\u524d\u9875\u9762\uff0c\u4e0d\u80fd\u8fd4\u56de\nwindow.location.replace(&quot;https:\/\/www.example.com&quot;);\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>5.&nbsp;<code>window.location.href<\/code>&nbsp;\u548c SPA\uff08\u5355\u9875\u9762\u5e94\u7528\uff09<\/strong><\/h3>\n\n\n\n<p>\u5728\u5355\u9875\u9762\u5e94\u7528\uff08SPA\uff09\u4e2d\uff0c<code>window.location.href<\/code>&nbsp;\u901a\u5e38\u7528\u4e8e\u9875\u9762\u4e4b\u95f4\u7684\u8df3\u8f6c\u3002\u5355\u9875\u9762\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u4f7f\u7528\u5ba2\u6237\u7aef\u8def\u7531\uff08\u5982 React Router\u3001Vue Router \u7b49\uff09\u6765\u5904\u7406 URL \u548c\u89c6\u56fe\u7684\u53d8\u5316\uff0c\u800c\u4e0d\u662f\u4f20\u7edf\u7684\u6d4f\u89c8\u5668\u9875\u9762\u5237\u65b0\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u4f1a\u7ed3\u5408\u4f7f\u7528&nbsp;<code>window.location.href<\/code>&nbsp;\u6216&nbsp;<code>window.location.assign()<\/code>&nbsp;\u6765\u5b9e\u73b0\u9875\u9762\u7684\u5b8c\u5168\u8df3\u8f6c\uff0c\u6216\u8005\u4f7f\u7528\u5ba2\u6237\u7aef\u8def\u7531\u6765\u907f\u514d\u9875\u9762\u5237\u65b0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u83b7\u53d6\u5f53\u524d URL<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>let currentUrl = window.location.href;<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u8df3\u8f6c\u5230\u65b0\u9875\u9762<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>window.location.href = \"https:\/\/www.example.com\";<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u91cd\u65b0\u52a0\u8f7d\u5f53\u524d\u9875\u9762<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>window.location.href = window.location.href;<\/code>\u00a0\u6216\u00a0<code>window.location.reload();<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u66ff\u4ee3\u5f53\u524d\u9875\u9762\uff08\u4e0d\u6dfb\u52a0\u5230\u5386\u53f2\u8bb0\u5f55\u4e2d\uff09<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>window.location.replace(\"https:\/\/www.example.com\");<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7&nbsp;<code>window.location.href<\/code>\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u6d4f\u89c8\u5668\u4e2d\u83b7\u53d6\u5f53\u524d URL\u3001\u8df3\u8f6c\u5230\u65b0\u9875\u9762\u6216\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\uff0c\u662f\u7f51\u9875\u7f16\u7a0b\u4e2d\u5e38\u7528\u7684\u4e00\u4e2a\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>window.location.href&nbsp;\u7684\u4ecb\u7ecd\u4e0e\u4f7f\u7528 window&#8230;. <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/11\/20\/window-location-href%e7%9a%84%e4%bb%8b%e7%bb%8d%e5%8f%8a%e4%bd%bf%e7%94%a8\/\">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":[5],"tags":[],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-html-xhtml"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/81","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=81"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":82,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions\/82"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}