{"id":836,"date":"2025-12-22T08:39:22","date_gmt":"2025-12-22T00:39:22","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=836"},"modified":"2025-12-22T08:39:22","modified_gmt":"2025-12-22T00:39:22","slug":"window-location-href%e7%9a%84%e4%bb%8b%e7%bb%8d%e5%8f%8a%e4%bd%bf%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/22\/window-location-href%e7%9a%84%e4%bb%8b%e7%bb%8d%e5%8f%8a%e4%bd%bf%e7%94%a8-2\/","title":{"rendered":"window.location.href\u7684\u4ecb\u7ecd\u53ca\u4f7f\u7528\u00a0"},"content":{"rendered":"\n<p>window.location.href \u662f JavaScript \u4e2d\u6700\u5e38\u7528\u7684\u5c5e\u6027\u4e4b\u4e00\uff0c\u7528\u4e8e\u83b7\u53d6\u6216\u8bbe\u7f6e\u5f53\u524d\u9875\u9762\u7684\u5b8c\u6574 URL\u30021. \u57fa\u672c\u4ecb\u7ecd<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6240\u5c5e\u5bf9\u8c61\uff1awindow.location\uff08location \u662f window \u7684\u5c5e\u6027\uff09<\/li>\n\n\n\n<li>\u7c7b\u578b\uff1a\u5b57\u7b26\u4e32\uff08string\uff09<\/li>\n\n\n\n<li>\u4f5c\u7528\uff1a\n<ul class=\"wp-block-list\">\n<li>\u8bfb\u53d6\uff1a\u83b7\u53d6\u5f53\u524d\u9875\u9762\u7684\u5b8c\u6574 URL\uff08\u5305\u62ec\u534f\u8bae\u3001\u57df\u540d\u3001\u8def\u5f84\u3001\u67e5\u8be2\u53c2\u6570\u3001\u951a\u70b9\u7b49\uff09<\/li>\n\n\n\n<li>\u5199\u5165\uff1a\u5c06\u65b0 URL \u8d4b\u503c\u7ed9\u5b83 \u2192 \u6d4f\u89c8\u5668\u7acb\u5373\u8df3\u8f6c\u5230\u8be5 URL\uff08\u76f8\u5f53\u4e8e\u9875\u9762\u91cd\u5b9a\u5411\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>MDN \u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/Location\/href\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/Location\/href<\/a>2. \u5e38\u89c1\u7528\u6cd5<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u7528\u6cd5<\/th><th>\u8bf4\u660e<\/th><th>\u793a\u4f8b\u4ee3\u7801<\/th><\/tr><tr><td>\u83b7\u53d6\u5f53\u524d URL<\/td><td>\u8fd4\u56de\u5b8c\u6574 URL \u5b57\u7b26\u4e32<\/td><td>console.log(window.location.href);<\/td><\/tr><tr><td>\u8df3\u8f6c\u5230\u65b0\u9875\u9762\uff08\u5f53\u524d\u7a97\u53e3\uff09<\/td><td>\u76f4\u63a5\u8d4b\u503c\u5373\u53ef\u8df3\u8f6c<\/td><td>window.location.href = &#8220;https:\/\/www.example.com&#8221;;<\/td><\/tr><tr><td>\u8df3\u8f6c\u5230\u76f8\u5bf9\u8def\u5f84<\/td><td>\u76f8\u5bf9\u4e8e\u5f53\u524d\u9875\u9762<\/td><td>window.location.href = &#8220;\/about&#8221;;<\/td><\/tr><tr><td>\u8df3\u8f6c\u5230\u5f53\u524d\u9875\u9762\uff08\u5237\u65b0\uff09<\/td><td>\u5e38\u7528\u4e8e\u5f3a\u5236\u5237\u65b0<\/td><td>window.location.href = window.location.href;<\/td><\/tr><tr><td>\u7701\u7565 window \u524d\u7f00<\/td><td>JavaScript \u5141\u8bb8<\/td><td>location.href = &#8220;https:\/\/baidu.com&#8221;;<\/td><\/tr><tr><td>\u76f4\u63a5\u8d4b\u503c\u7ed9 location\uff08\u7b49\u6548\uff09<\/td><td>\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u89e3\u6790\u4e3a location.href<\/td><td>location = &#8220;https:\/\/google.com&#8221;;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>3. \u5b9e\u9645\u4ee3\u7801\u793a\u4f8b<\/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\/\/ 1. \u83b7\u53d6\u5f53\u524d\u5b8c\u6574 URL\nconsole.log(location.href); \n\/\/ \u8f93\u51fa\u793a\u4f8b: https:\/\/example.com\/path?name=\u5f20\u4e09#section1\n\n\/\/ 2. \u9875\u9762\u8df3\u8f6c\uff08\u6700\u5e38\u7528\uff09\nfunction gotoPage() {\n  location.href = &quot;https:\/\/www.baidu.com&quot;;  \/\/ \u8df3\u8f6c\u5230\u767e\u5ea6\n}\n\n\/\/ 3. \u5e26\u53c2\u6570\u8df3\u8f6c\nlocation.href = &quot;\/search?keyword=&quot; + encodeURIComponent(&quot;JavaScript location&quot;);\n\n\/\/ 4. \u8df3\u8f6c\u540e\u65e0\u6cd5\u8fd4\u56de\uff08\u4e0d\u63a8\u8350\u65e5\u5e38\u4f7f\u7528\uff09\nlocation.replace(&quot;https:\/\/www.example.com&quot;);  \/\/ \u66ff\u6362\u5386\u53f2\u8bb0\u5f55\uff0c\u7528\u6237\u65e0\u6cd5\u201c\u540e\u9000\u201d\n\n\/\/ 5. \u5b89\u5168\u8df3\u8f6c\uff08\u63a8\u8350\u73b0\u4ee3\u5199\u6cd5\uff09\nlocation.assign(&quot;https:\/\/www.example.com&quot;);   \/\/ \u7b49\u4ef7\u4e8e location.href\uff0c\u4f46\u66f4\u53ef\u8bfb\u4e14\u6613\u6d4b\u8bd5\n<\/pre><\/div>\n\n\n<p>4. \u4e0e\u5176\u4ed6\u8df3\u8f6c\u65b9\u5f0f\u7684\u5bf9\u6bd4\uff08\u9762\u8bd5\u5e38\u8003\uff09<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u65b9\u6cd5\/\u5c5e\u6027<\/th><th>\u662f\u5426\u52a0\u5165\u5386\u53f2\u8bb0\u5f55<\/th><th>\u662f\u5426\u53ef\u540e\u9000<\/th><th>\u63a8\u8350\u573a\u666f<\/th><\/tr><tr><td>location.href = url<\/td><td>\u662f<\/td><td>\u53ef\u4ee5<\/td><td>\u666e\u901a\u9875\u9762\u8df3\u8f6c\uff08\u6700\u5e38\u7528\uff09<\/td><\/tr><tr><td>location.assign(url)<\/td><td>\u662f<\/td><td>\u53ef\u4ee5<\/td><td>\u73b0\u4ee3\u63a8\u8350\u5199\u6cd5\uff0c\u6613\u4e8e\u5355\u5143\u6d4b\u8bd5<\/td><\/tr><tr><td>location.replace(url)<\/td><td>\u5426<\/td><td>\u4e0d\u53ef<\/td><td>\u767b\u5f55\u6210\u529f\u540e\u8df3\u8f6c\u3001404 \u91cd\u5b9a\u5411<\/td><\/tr><tr><td>location.reload()<\/td><td>\u2014<\/td><td>\u2014<\/td><td>\u5237\u65b0\u5f53\u524d\u9875\u9762<\/td><\/tr><tr><td>window.open(url)<\/td><td>\u65b0\u7a97\u53e3\/\u6807\u7b7e<\/td><td>\u2014<\/td><td>\u6253\u5f00\u65b0\u6807\u7b7e\u9875<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>5. \u5e38\u89c1\u6ce8\u610f\u70b9<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8de8\u57df\u9650\u5236\uff1a\u76f4\u63a5\u64cd\u4f5c location.href \u53ea\u5728\u540c\u6e90\u9875\u9762\u6709\u6548\uff08\u540c\u534f\u8bae+\u57df\u540d+\u7aef\u53e3\uff09\u3002<\/li>\n\n\n\n<li>\u76f8\u5bf9\u8def\u5f84\uff1alocation.href = &#8220;page.html&#8221; \u76f8\u5bf9\u4e8e\u5f53\u524d\u8def\u5f84\u8df3\u8f6c\u3002<\/li>\n\n\n\n<li>\u951a\u70b9\uff08hash\uff09\uff1alocation.href = &#8220;#top&#8221; \u4e0d\u4f1a\u89e6\u53d1\u9875\u9762\u5237\u65b0\uff0c\u4f46\u4f1a\u6eda\u52a8\u5230\u951a\u70b9\u3002<\/li>\n\n\n\n<li>\u5237\u65b0\u9875\u9762\uff1alocation.href = location.href \u6216 location.reload()\u3002<\/li>\n\n\n\n<li>iframe \/ frameset\uff1a\u53ef\u80fd\u9700\u8981 parent.location.href \u6216 top.location.href \u6765\u63a7\u5236\u7236\/\u9876\u5c42\u7a97\u53e3\u3002<\/li>\n<\/ul>\n\n\n\n<p>6. \u6700\u4f73\u5b9e\u8df5<\/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\/\/ \u63a8\u8350\u5199\u6cd5\uff1a\u6e05\u6670\u4e14\u6613\u6d4b\u8bd5\nfunction redirectToHome() {\n  window.location.assign(&quot;\/&quot;);  \/\/ \u6216 location.assign(&quot;\/&quot;)\n}\n\n\/\/ \u66ff\u4ee3\u5199\u6cd5\uff08\u4f20\u7edf\u4e14\u66f4\u5e38\u89c1\uff09\nfunction redirectToHome() {\n  location.href = &quot;\/&quot;;\n}\n<\/pre><\/div>\n\n\n<p>\u5e0c\u671b\u8fd9\u7bc7\u603b\u7ed3\u80fd\u5e2e\u4f60\u5feb\u901f\u638c\u63e1 window.location.href \u7684\u4f7f\u7528\uff01\u5982\u679c\u6709\u5177\u4f53\u573a\u666f\uff0c\u4e5f\u53ef\u4ee5\u544a\u8bc9\u6211\uff0c\u6211\u518d\u5e2e\u4f60\u5199\u9488\u5bf9\u6027\u7684\u4ee3\u7801\u793a\u4f8b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>window.location.href \u662f JavaScript \u4e2d\u6700\u5e38\u7528\u7684\u5c5e&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/22\/window-location-href%e7%9a%84%e4%bb%8b%e7%bb%8d%e5%8f%8a%e4%bd%bf%e7%94%a8-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":[12],"tags":[],"class_list":["post-836","post","type-post","status-publish","format-standard","hentry","category-12"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/836","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=836"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/836\/revisions"}],"predecessor-version":[{"id":837,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/836\/revisions\/837"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}