{"id":169684,"date":"2024-05-08T16:51:02","date_gmt":"2024-05-08T08:51:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/169684.html"},"modified":"2024-05-08T16:51:05","modified_gmt":"2024-05-08T08:51:05","slug":"javascript-%e5%b0%86-url-%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e4%b8%ba%e8%b6%85%e9%93%be%e6%8e%a5%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/169684.html","title":{"rendered":"JavaScript \u5c06 URL \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\u7684\u95ee\u9898"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27040214\/ad50a2ba-ebb0-43e2-a233-4d74f16fb385.webp\" alt=\"JavaScript \u5c06 URL \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\u7684\u95ee\u9898\" \/><\/p>\n<p><p>\u5c06URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\u7684\u95ee\u9898\u9700\u8981\u8003\u8651\u7684\u5173\u952e\u70b9\u5305\u62ec\uff1a<strong>\u6587\u672c\u5339\u914d\u3001HTML\u8d85\u94fe\u63a5\u7684\u6784\u9020\u3001\u5141\u8bb8\u534f\u8bae\u3001\u5b89\u5168\u6027\u5904\u7406\u3001\u517c\u5bb9\u6027<\/strong>\u3002\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u901a\u5e38\u91c7\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914dURL\uff0c\u7136\u540e\u521b\u5efa\u76f8\u5e94\u7684<code>&lt;a&gt;<\/code>\u6807\u7b7e\u4ee5\u5d4c\u5165\u5230\u7f51\u9875\u4e2d\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\u4fdd\u8bc1\u8f6c\u6362\u540e\u7684\u94fe\u63a5\u4e0d\u4ec5\u80fd\u6b63\u786e\u5de5\u4f5c\uff0c\u540c\u65f6\u4e5f\u4e0d\u5f15\u5165\u5b89\u5168\u98ce\u9669\uff0c\u4f8b\u5982\u907f\u514dXSS\u653b\u51fb\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u91cd\u70b9\u63cf\u8ff0<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u8fc7\u7a0b<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001URL\u68c0\u6d4b\u4e0e\u5339\u914d<\/h3>\n<\/p>\n<p><h4>\u6b63\u5219\u8868\u8fbe\u5f0f\u6784\u5efa<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c06URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\uff0c\u9996\u5148\u9700\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u5339\u914d\u3002\u4e00\u4e2a\u7b80\u5355\u6709\u6548\u7684URL\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const urlRegex = \/(\\bhttps?:\\\/\\\/[^\\s&lt;]+)|(www\\.[^\\s&lt;]+?\\.[^\\s&lt;]+)\/g;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u80fd\u591f\u8bc6\u522b\u5927\u591a\u6570\u4ee5http\u6216https\u5f00\u5934\u7684URL\uff0c\u4ee5\u53ca\u5e38\u89c1\u7684\u4e0d\u5e26\u534f\u8bae\u4f46\u4ee5www\u5f00\u5934\u7684\u7f51\u5740\u3002<\/p>\n<\/p>\n<p><h4>\u6b63\u5219\u6d4b\u8bd5\u4e0e\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u6d4b\u8bd5\uff0c\u53ef\u80fd\u4f1a\u53d1\u73b0\u4e00\u4e9b\u8fb9\u754c\u60c5\u51b5\u672a\u88ab\u8986\u76d6\uff0c\u6216\u8005\u4e00\u4e9b\u975eURL\u5b57\u7b26\u4e32\u88ab\u8bef\u5339\u914d\u3002\u4e3a\u4e86\u63d0\u9ad8\u5339\u914d\u8d28\u91cf\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u6b63\u5219\uff0c\u6dfb\u52a0\u66f4\u591a\u7684\u5339\u914d\u89c4\u5219\uff0c\u6216\u8005\u9650\u5b9a\u5339\u914d\u4e0a\u4e0b\u6587\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u7aef\u53e3\u53f7\u7684\u5339\u914d\uff0c\u6216\u5bf9\u672b\u5c3e\u5b57\u7b26\u8fdb\u884c\u9650\u5236\uff0c\u4ee5\u51cf\u5c11\u8bef\u5339\u914d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001HTML\u8d85\u94fe\u63a5\u6784\u9020<\/h3>\n<\/p>\n<p><h4>\u94fe\u63a5\u6807\u7b7e\u7684\u751f\u6210<\/h4>\n<\/p>\n<p><p>\u5339\u914d\u5230URL\u540e\uff0c\u9700\u8981\u5c06\u5176\u6784\u9020\u6210\u4e00\u4e2a\u6807\u51c6\u7684HTML\u8d85\u94fe\u63a5\u3002\u5904\u7406\u65b9\u5f0f\u662f\u901a\u8fc7JavaScript\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u529f\u80fd\uff0c\u5c06\u6587\u672c\u4e2d\u7684URL\u66ff\u6362\u4e3a<code>&lt;a&gt;<\/code>\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function convertToAnchor(text) {<\/p>\n<p>    return text.replace(urlRegex, function(url) {<\/p>\n<p>        const protocol = url.startsWith(&#039;http&#039;) ? &#039;&#039; : &#039;http:\/\/&#039;;<\/p>\n<p>        return &#039;&lt;a href=&quot;&#039; + protocol + url + &#039;&quot; target=&quot;_blank&quot;&gt;&#039; + url + &#039;&lt;\/a&gt;&#039;;<\/p>\n<p>    });<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u8fd8\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5224\u65ad\uff0c\u5982\u679cURL\u4e0d\u5305\u542b\u534f\u8bae\u90e8\u5206\uff0c\u5c31\u9ed8\u8ba4\u6dfb\u52a0<code>&#039;http:\/\/&#039;<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u7279\u6b8a\u5b57\u7b26\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728URL\u4e2d\u53ef\u80fd\u5305\u542b\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\uff0c\u5b83\u4eec\u5728HTML\u4e2d\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u6bd4\u5982<code>&amp;<\/code>\u3001<code>&quot;<\/code>\u3001<code>&lt;<\/code>\u3001<code>&gt;<\/code>\u7b49\u3002\u5728\u751f\u6210\u94fe\u63a5\u6807\u7b7e\u4e4b\u524d\uff0c\u8981\u5bf9\u8fd9\u4e9b\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\u5904\u7406\uff0c\u907f\u514dXSS\u653b\u51fb\u6216\u662f\u6807\u7b7e\u89e3\u6790\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5141\u8bb8\u534f\u8bae\u7684\u5904\u7406<\/h3>\n<\/p>\n<p><h4>\u534f\u8bae\u7684\u6821\u9a8c<\/h4>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u5e94\u7528\u573a\u666f\u4e0b\uff0c\u53ef\u80fd\u53ea\u60f3\u5141\u8bb8\u7279\u5b9a\u534f\u8bae\u7684URL\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\u3002\u4e3a\u6b64\uff0c\u53ef\u4ee5\u5728URL\u5339\u914d\u4e0e\u66ff\u6362\u9636\u6bb5\u52a0\u5165\u534f\u8bae\u7684\u6821\u9a8c\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const allowedProtocols = [&#039;http:&#039;, &#039;https:&#039;];<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u66ff\u6362\u51fd\u6570\u4e2d\u6dfb\u52a0\u534f\u8bae\u6821\u9a8c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function protocolCheck(url) {<\/p>\n<p>    const urlObj = new URL(url);<\/p>\n<p>    return allowedProtocols.includes(urlObj.protocol);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u975e\u6cd5\u534f\u8bae\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5982\u679cURL\u5305\u542b\u975e\u6cd5\u534f\u8bae\uff0c\u5e94\u6309\u7167\u8bbe\u5b9a\u7684\u7b56\u7565\u8fdb\u884c\u5904\u7406\uff0c\u53ef\u80fd\u662f\u76f4\u63a5\u5ffd\u7565\u8be5URL\uff0c\u4e5f\u53ef\u80fd\u662f\u5c06\u5176\u66ff\u6362\u4e3a\u5b89\u5168\u534f\u8bae\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b89\u5168\u6027\u5904\u7406<\/h3>\n<\/p>\n<p><h4>XSS\u653b\u51fb\u9632\u5fa1<\/h4>\n<\/p>\n<p><p>\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\u7684URL\u53ef\u80fd\u4f1a\u88ab\u6076\u610f\u5229\u7528\u6267\u884c\u811a\u672c\uff0c\u4ece\u800c\u51fa\u73b0\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff08Cross-Site-Scripting, XSS\uff09\u3002\u786e\u4fdd\u53ea\u6709\u5408\u6cd5\u7684URL\u88ab\u8f6c\u6362\u5e76\u4e14\u7279\u6b8a\u5b57\u7b26\u88ab\u6b63\u786e\u8f6c\u4e49\uff0c\u662f\u9632\u6b62XSS\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function escapeHTML(text) {<\/p>\n<p>    var escapeMap = {<\/p>\n<p>        &#039;&amp;&#039;: &#039;&amp;amp;&#039;,<\/p>\n<p>        &#039;&lt;&#039;: &#039;&amp;lt;&#039;,<\/p>\n<p>        &#039;&gt;&#039;: &#039;&amp;gt;&#039;,<\/p>\n<p>        &#039;&quot;&#039;: &#039;&amp;quot;&#039;,<\/p>\n<p>        &quot;&#039;&quot;: &#039;&#039;&#039;<\/p>\n<p>    };<\/p>\n<p>    return text.replace(\/[&amp;&lt;&gt;&quot;&#039;]\/g, function(m) { return escapeMap[m]; });<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728URL\u8f6c\u6362\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>escapeHTML<\/code>\u51fd\u6570\u5bf9\u6587\u672c\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<\/p>\n<p><h4>\u76ee\u6807\u5c5e\u6027\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u9632\u6b62\u70b9\u51fb\u8d85\u94fe\u63a5\u540e\u9875\u9762\u5185\u5bb9\u88ab\u66ff\u6362\uff0c\u901a\u5e38\u4f1a\u8bbe\u7f6e\u8d85\u94fe\u63a5\u7684<code>target<\/code>\u5c5e\u6027\u4e3a<code>_blank<\/code>\uff0c\u540c\u7406\uff0c\u8fd8\u5e94\u8be5\u8bbe\u7f6e<code>rel=&quot;noopener noreferrer&quot;<\/code>\u907f\u514d<code>target=&quot;_blank&quot;<\/code>\u7684\u5b89\u5168\u6f0f\u6d1e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u517c\u5bb9\u6027\u4e0e\u6548\u7387<\/h3>\n<\/p>\n<p><h4>\u6d4f\u89c8\u5668\u7684\u652f\u6301<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u4f7f\u7528\u7684JavaScript\u65b9\u6cd5\u5728\u4e3b\u6d41\u7684\u6d4f\u89c8\u5668\u4e2d\u90fd\u5f97\u5230\u826f\u597d\u7684\u652f\u6301\u3002\u5bf9\u4e8e\u4e00\u4e9b\u8f83\u65b0\u7684API\uff0c\u5982<code>URL<\/code>\u7c7b\uff0c\u5728\u8001\u7248\u672c\u7684\u6d4f\u89c8\u5668\u4e2d\u53ef\u80fd\u4e0d\u88ab\u652f\u6301\uff0c\u9700\u8981\u63d0\u4f9b\u76f8\u5e94\u7684polyfill\u4f5c\u4e3a\u540e\u5907\u3002<\/p>\n<\/p>\n<p><h4>\u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u7684\u6587\u672c\u5185\u5bb9\u65f6\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u548c\u66ff\u6362\u53ef\u80fd\u4f1a\u6210\u4e3a\u6027\u80fd\u74f6\u9888\u3002\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6548\u7387\uff0c\u6bd4\u5982\u51cf\u5c11\u56de\u6eaf\uff0c\u6216\u8005\u5c06\u5904\u7406\u62c6\u5206\u4e3a\u591a\u4e2a\u6b65\u9aa4\uff0c\u751a\u81f3\u4f7f\u7528Web Workers\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u90fd\u662f\u53ef\u884c\u7684\u4f18\u5316\u63aa\u65bd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q\uff1a\u5982\u4f55\u4f7f\u7528JavaScript\u5c06URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\uff1f<\/strong><\/p>\n<p>A\uff1a\u8981\u5c06URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\uff0c\u53ef\u4ee5\u4f7f\u7528JavaScript\u7684createElement\u548csetAttribute\u65b9\u6cd5\u6765\u52a8\u6001\u521b\u5efa\u4e00\u4e2a<a>\u6807\u7b7e\uff0c\u5e76\u5c06URL\u4f5c\u4e3a\u94fe\u63a5\u5730\u5740\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5c06\u521b\u5efa\u7684<a>\u6807\u7b7e\u63d2\u5165\u5230\u9875\u9762\u7684\u67d0\u4e2a\u7279\u5b9a\u4f4d\u7f6e\uff0c\u6216\u8005\u7528\u5b83\u66ff\u6362\u5df2\u6709\u7684\u6587\u672c\u3002<\/p>\n<p><strong>Q\uff1a\u600e\u6837\u7528JavaScript\u5c06URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u53ef\u70b9\u51fb\u7684\u8d85\u94fe\u63a5\u6587\u672c\uff1f<\/strong><\/p>\n<p>A\uff1a\u4f7f\u7528JavaScript\u5c06URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u53ef\u70b9\u51fb\u7684\u8d85\u94fe\u63a5\u6587\u672c\u5f88\u7b80\u5355\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u65b9\u5f0f\uff0c\u5c06URL\u5730\u5740\u5305\u88f9\u5728<a>\u6807\u7b7e\u4e2d\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7JavaScript\u7684innerHTML\u5c5e\u6027\uff0c\u5c06\u8fd9\u4e2a\u5305\u542b<a>\u6807\u7b7e\u7684\u5b57\u7b26\u4e32\u63d2\u5165\u5230\u9875\u9762\u7684\u4efb\u610f\u4f4d\u7f6e\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u5c06URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u53ef\u70b9\u51fb\u7684\u8d85\u94fe\u63a5\u6587\u672c\u3002<\/p>\n<p><strong>Q\uff1a\u80fd\u5426\u7528JavaScript\u81ea\u52a8\u5c06\u9875\u9762\u4e2d\u7684\u5168\u90e8URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\uff1f<\/strong><\/p>\n<p>A\uff1a\u5f53\u7136\u53ef\u4ee5\uff01\u4f7f\u7528JavaScript\u53ef\u4ee5\u5f88\u8f7b\u677e\u5730\u5b9e\u73b0\u81ea\u52a8\u5c06\u9875\u9762\u4e2d\u7684\u5168\u90e8URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528JavaScript\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u6587\u672c\u4e2d\u7684URL\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u7684\u65b9\u6cd5\uff0c\u5c06\u5339\u914d\u7684URL\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a\u5e26\u6709<a>\u6807\u7b7e\u7684\u8d85\u94fe\u63a5\u6587\u672c\u3002\u53ef\u4ee5\u4f7f\u7528innerHTML\u5c5e\u6027\u5c06\u66ff\u6362\u540e\u7684\u6587\u672c\u63d2\u5165\u5230\u9875\u9762\u4e2d\uff0c\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u81ea\u52a8\u5c06\u9875\u9762\u4e2d\u7684\u5168\u90e8URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06URL\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8d85\u94fe\u63a5\u7684\u95ee\u9898\u9700\u8981\u8003\u8651\u7684\u5173\u952e\u70b9\u5305\u62ec\uff1a\u6587\u672c\u5339\u914d\u3001HTML\u8d85\u94fe\u63a5\u7684\u6784\u9020\u3001\u5141\u8bb8\u534f\u8bae\u3001\u5b89\u5168\u6027\u5904\u7406\u3001\u517c [&hellip;]","protected":false},"author":3,"featured_media":169685,"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\/169684"}],"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=169684"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169684\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/169685"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=169684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=169684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=169684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}