{"id":309981,"date":"2024-05-20T21:39:15","date_gmt":"2024-05-20T13:39:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/309981.html"},"modified":"2024-05-20T21:39:35","modified_gmt":"2024-05-20T13:39:35","slug":"js-%e7%bc%96%e7%a8%8b%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96%e6%b5%8f%e8%a7%88%e5%99%a8%e7%9a%84%e5%8f%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/309981.html","title":{"rendered":"js \u7f16\u7a0b\u600e\u4e48\u83b7\u53d6\u6d4f\u89c8\u5668\u7684\u53c2\u6570"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26230505\/848405c6-e7df-428f-b02e-df6c1907e352.webp\" alt=\"js \u7f16\u7a0b\u600e\u4e48\u83b7\u53d6\u6d4f\u89c8\u5668\u7684\u53c2\u6570\" \/><\/p>\n<p><p>\u83b7\u53d6\u6d4f\u89c8\u5668\u7684\u53c2\u6570\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7<strong>JavaScript\u7f16\u7a0b\u6765\u5b9e\u73b0\u3001\u83b7\u53d6URL\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u53c2\u6570\u3001\u5229\u7528navigator\u5bf9\u8c61\u83b7\u53d6\u7528\u6237\u4ee3\u7406\u4fe1\u606f\u3001\u901a\u8fc7screen\u5bf9\u8c61\u83b7\u53d6\u5c4f\u5e55\u5c3a\u5bf8\u3001\u4f7f\u7528location\u5bf9\u8c61\u64cd\u4f5c\u548c\u83b7\u53d6URL\u5404\u90e8\u5206\u4fe1\u606f<\/strong>\u3002\u4f8b\u5982\uff0c\u4f7f\u7528JavaScript\u5185\u7f6e\u7684<code>URLSearchParams<\/code>\u5bf9\u8c61\u53ef\u4ee5\u65b9\u4fbf\u5730\u89e3\u6790URL\u4e2d\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\uff08\u5373\u95ee\u53f7<code>?<\/code>\u540e\u9762\u7684\u90e8\u5206\uff09\uff0c\u6765\u83b7\u53d6\u53c2\u6570\u7684\u503c\uff0c\u5e76\u5bf9\u8fd9\u4e9b\u503c\u8fdb\u884c\u64cd\u4f5c\u3002\u8fd9\u662f\u5b9e\u73b0\u83b7\u53d6\u6d4f\u89c8\u5668\u53c2\u6570\u7684\u5e38\u89c1\u548c\u6807\u51c6\u505a\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u83b7\u53d6URL\u67e5\u8be2\u5b57\u7b26\u4e32\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u83b7\u53d6\u7528\u6237\u901a\u8fc7\u6d4f\u89c8\u5668\u5730\u5740\u680f\u8f93\u5165\u7684\u67e5\u8be2\u53c2\u6570\uff0c\u6bd4\u5982\u5728\u4e00\u4e2aURL\u4e3a &quot;<a href=\"http:\/\/example.com?page=3&amp;sort=ascending\">http:\/\/example.com?page=3&amp;sort=ascending<\/a>&quot; \u7684\u9875\u9762\u4e0a\uff0c\u83b7\u53d6&quot;page&quot;\u548c&quot;sort&quot;\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u83b7\u53d6\u5355\u4e2a\u67e5\u8be2\u53c2\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">const params = new URLSearchParams(window.location.search);<\/p>\n<p>const page = params.get(&#039;page&#039;); \/\/ \u5f97\u5230 &quot;3&quot;<\/p>\n<p>const sort = params.get(&#039;sort&#039;); \/\/ \u5f97\u5230 &quot;ascending&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u6240\u6709\u67e5\u8be2\u53c2\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">const allParams = {};<\/p>\n<p>params.forEach((value, key) =&gt; {<\/p>\n<p>  allParams[key] = value;<\/p>\n<p>});<\/p>\n<p>\/\/ allParams \u73b0\u5728\u662f { page: &quot;3&quot;, sort: &quot;ascending&quot; }<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528navigator\u5bf9\u8c61\u83b7\u53d6\u7528\u6237\u4ee3\u7406\u4fe1\u606f<\/h3>\n<\/p>\n<p><p><code>navigator<\/code> \u5bf9\u8c61\u662fwindow\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u5305\u542b\u4e86\u7528\u6237\u7684\u6d4f\u89c8\u5668\u4fe1\u606f\uff0c\u6bd4\u5982\u6d4f\u89c8\u5668\u7684\u540d\u79f0\u3001\u7248\u672c\u53f7\u548c\u64cd\u4f5c\u7cfb\u7edf\u7b49\u3002\u5e38\u7528\u4e8e\u786e\u5b9a\u8bbf\u95ee\u8005\u6b63\u5728\u4f7f\u7528\u4ec0\u4e48\u7c7b\u578b\u7684\u6d4f\u89c8\u5668\u6216\u8bbe\u5907\u3002<\/p>\n<\/p>\n<p><h4>\u83b7\u53d6\u6d4f\u89c8\u5668\u540d\u79f0\u548c\u7248\u672c<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">const userAgent = navigator.userAgent;<\/p>\n<p>const browserName = navigator.appName;<\/p>\n<p>const browserVersion = navigator.appVersion;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u68c0\u6d4b\u6d4f\u89c8\u5668\u662f\u5426\u5728\u7ebf<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">const isOnline = navigator.onLine; \/\/ \u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7screen\u5bf9\u8c61\u83b7\u53d6\u5c4f\u5e55\u5c3a\u5bf8<\/h3>\n<\/p>\n<p><p><code>screen<\/code> \u5bf9\u8c61\u5305\u542b\u4e86\u4e0e\u5ba2\u6237\u7aef\u7684\u663e\u793a\u5c4f\u5e55\u6709\u5173\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\u7528\u6237\u7684\u663e\u793a\u5668\u5c4f\u5e55\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>\u83b7\u53d6\u5c4f\u5e55\u4fe1\u606f<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">const screenWidth = screen.width;<\/p>\n<p>const screenHeight = screen.height;<\/p>\n<p>const av<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lableWidth = screen.availWidth; \/\/ \u53ef\u7528\u7684\u5c4f\u5e55\u5bbd\u5ea6<\/p>\n<p>const availableHeight = screen.availHeight; \/\/ \u53ef\u7528\u7684\u5c4f\u5e55\u9ad8\u5ea6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528location\u5bf9\u8c61\u64cd\u4f5c\u548c\u83b7\u53d6URL\u5404\u90e8\u5206\u4fe1\u606f<\/h3>\n<\/p>\n<p><p><code>location<\/code> \u5bf9\u8c61\u63d0\u4f9b\u4e86\u5173\u4e8e\u5f53\u524dURL\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u88ab\u7528\u6765\u89e3\u6790URL\u7684\u90e8\u5206\uff0c\u6bd4\u5982\u534f\u8bae\u3001\u4e3b\u673a\u540d\u3001\u8def\u5f84\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u83b7\u53d6URL\u7684\u90e8\u5206\u4fe1\u606f<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">const href = location.href; \/\/ \u5f53\u524dURL<\/p>\n<p>const protocol = location.protocol; \/\/ \u6bd4\u5982 &quot;http:&quot;<\/p>\n<p>const host = location.host; \/\/ \u4e3b\u673a\u540d\u548c\u7aef\u53e3\u53f7<\/p>\n<p>const hostname = location.hostname; \/\/ \u4e3b\u673a\u540d\u4e0d\u5305\u62ec\u7aef\u53e3\u53f7<\/p>\n<p>const port = location.port; \/\/ \u7aef\u53e3\u53f7<\/p>\n<p>const pathname = location.pathname; \/\/ URL\u7684\u8def\u5f84\u90e8\u5206<\/p>\n<p>const search = location.search; \/\/ \u67e5\u8be2\u5b57\u7b26\u4e32<\/p>\n<p>const hash = location.hash; \/\/ \u951a\u70b9\u90e8\u5206<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4fee\u6539URL\u7684\u90e8\u5206\u4fe1\u606f<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">location.assign(&#039;http:\/\/www.example.com&#039;); \/\/ \u52a0\u8f7d\u65b0\u9875\u9762<\/p>\n<p>location.replace(&#039;http:\/\/www.example.com&#039;); \/\/ \u66ff\u6362\u5f53\u524d\u9875\u9762\uff0c\u4e0d\u4f1a\u5728\u5386\u53f2\u8bb0\u5f55\u4e2d\u7559\u4e0b\u8bb0\u5f55<\/p>\n<p>location.reload(); \/\/ \u91cd\u65b0\u52a0\u8f7d\u5f53\u524d\u9875\u9762<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cJavaScript\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u6765\u83b7\u53d6\u548c\u64cd\u4f5c\u6d4f\u89c8\u5668\u7684\u53c2\u6570\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7ed3\u5408\u5177\u4f53\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u83b7\u53d6\u6216\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728 JavaScript \u7f16\u7a0b\u4e2d\u83b7\u53d6\u6d4f\u89c8\u5668\u7684\u53c2\u6570\uff1f<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>window.location.search<\/code> \u6765\u83b7\u53d6\u6d4f\u89c8\u5668\u7684\u53c2\u6570\u3002\u8fd9\u4e2a\u5c5e\u6027\u8fd4\u56de URL \u4e2d\u7684\u67e5\u8be2\u53c2\u6570\u90e8\u5206\uff0c\u5305\u62ec\u95ee\u53f7\uff08?\uff09\u548c\u540e\u9762\u7684\u53c2\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u89e3\u6790\u8fd9\u4e2a\u5b57\u7b26\u4e32\u6765\u83b7\u53d6\u7279\u5b9a\u7684\u53c2\u6570\u503c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 JavaScript \u4e2d\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\uff0c\u6bd4\u5982 <code>split()<\/code>\u3001<code>substring()<\/code> \u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u53c2\u6570\u503c\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u83b7\u53d6\u7279\u5b9a\u7684\u6d4f\u89c8\u5668\u53c2\u6570\u503c\uff1f<\/strong><\/p>\n<p>\u4e00\u65e6\u4f60\u83b7\u53d6\u5230\u6d4f\u89c8\u5668\u7684\u53c2\u6570\u5b57\u7b26\u4e32\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u6765\u83b7\u53d6\u7279\u5b9a\u7684\u53c2\u6570\u503c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u6839\u636e\u7279\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u53c2\u6570\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u952e\u503c\u5bf9\u6570\u7ec4\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u6bd4\u8f83\u6bcf\u4e2a\u952e\u4e0e\u4f60\u6240\u9700\u7684\u53c2\u6570\u952e\u3002\u5f53\u5339\u914d\u5230\u76f8\u5e94\u7684\u952e\u65f6\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u548c\u63d0\u53d6\u53c2\u6570\u503c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5\uff0c\u5982 <code>match()<\/code> \u6216 <code>exec()<\/code> \u6765\u5339\u914d\u53c2\u6570\u952e\u548c\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u590d\u6742\u7684\u53c2\u6570\u7ed3\u6784\u6216\u9700\u8981\u7075\u6d3b\u5339\u914d\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>3. \u662f\u5426\u6709\u73b0\u6210\u7684 JavaScript \u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u83b7\u53d6\u6d4f\u89c8\u5668\u53c2\u6570\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0cJavaScript \u4e2d\u6709\u4e00\u4e9b\u73b0\u6210\u7684\u51fd\u6570\u6216\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u83b7\u53d6\u6d4f\u89c8\u5668\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>URLSearchParams<\/code> API \u6216\u7b2c\u4e09\u65b9\u5e93\u5982 <code>query-string<\/code> \u6765\u7b80\u5316\u64cd\u4f5c\u3002<code>URLSearchParams<\/code> \u5141\u8bb8\u4f60\u4ee5\u5bf9\u8c61\u7684\u5f62\u5f0f\u8bbf\u95eeURL\u7684\u67e5\u8be2\u53c2\u6570\uff0c\u4ece\u800c\u66f4\u65b9\u4fbf\u5730\u83b7\u53d6\u548c\u64cd\u4f5c\u53c2\u6570\u503c\u3002<code>query-string<\/code> \u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5305\u62ec\u89e3\u6790\u548c\u5b57\u7b26\u4e32\u5316\u67e5\u8be2\u53c2\u6570\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u51cf\u5c11\u4f60\u624b\u52a8\u5904\u7406\u53c2\u6570\u5b57\u7b26\u4e32\u7684\u5de5\u4f5c\u91cf\uff0c\u5e76\u63d0\u4f9b\u66f4\u53ef\u9760\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u6d4f\u89c8\u5668\u53c2\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u83b7\u53d6\u6d4f\u89c8\u5668\u7684\u53c2\u6570\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7JavaScript\u7f16\u7a0b\u6765\u5b9e\u73b0\u3001\u83b7\u53d6URL\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u53c2\u6570\u3001\u5229\u7528navigator [&hellip;]","protected":false},"author":3,"featured_media":310011,"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\/309981"}],"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=309981"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/309981\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/310011"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=309981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=309981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=309981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}