{"id":9227,"date":"2020-01-17T23:14:20","date_gmt":"2020-01-17T15:14:20","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9227"},"modified":"2020-01-17T23:18:06","modified_gmt":"2020-01-17T15:18:06","slug":"js-relative-absolute-path","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/01\/js-relative-absolute-path\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528JS\u628a\u76f8\u5bf9\u5730\u5740\u53d8\u6210\u7edd\u5bf9\u5730\u5740\uff1f"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9227\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9227<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202001-url-to-abs.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/202001\/url-rel-abs.jpg\" width=\"480\" height=\"300\" alt=\"\u6587\u7ae0\u5934\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u573a\u666f\uff0c\u77e5\u9053\u4e00\u4e2a\u76f8\u5bf9\u5730\u5740\uff0c\u5e0c\u671b\u8f6c\u6362\u6210\u7edd\u5bf9\u5b9a\u4f4d\u3002\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>&#8220;\/wordpress<wbr>\/?p=9227&#8243; \u2192 &#8220;https:\/\/<wbr>www.zhangxinxu.com<wbr>\/wordpress<wbr>\/?p=9227&#8243;<\/li>\n<li>&#8220;..\/images\/zhangxinxu.png&#8221; \u2192 &#8220;https:\/\/<wbr>image.zhangxinxu.com<wbr>\/blog<wbr>\/images<wbr>\/zhangxinxu.png&#8221;<\/li>\n<\/ul>\n<p>\u6709\u6ca1\u6709\u4ec0\u4e48\u529e\u6cd5\u53ef\u4ee5\u548c\u5feb\u901f\u5730\u5b9e\u73b0\u76f8\u5bf9\u5730\u5740\u5230\u7edd\u5bf9\u5730\u5740\u7684\u8f6c\u6362\u5462\uff1f<\/p>\n<p>\u4e0b\u9762\u4ecb\u7ecd\u4e24\u4e2a\u6211\u6240\u77e5\u9053\u7684\u65b9\u6cd5\u3002<\/p>\n<h3>\u4e00\u3001new URL()\u65b9\u6cd5<\/h3>\n<p>\u6709\u65f6\u5019\u4e3a\u4e86\u65b9\u4fbf\u7684\u5904\u7406URL\u5730\u5740\uff0c\u4f1a\u4f7f\u7528<code>URL()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>var myUrl = new URL('https:\/\/www.zhangxinxu.com\/wordpress\/?p=9227');\r\n<span style=\"color:green;\">\/\/ \u53ef\u4ee5\u65b9\u4fbf\u83b7\u5f97URL\u5730\u5740\u7684\u5404\u4e2a\u7ec4\u6210\u90e8\u5206<\/span>\r\nconsole.log(myUrl.host);\r\nconsole.log(myUrl.path);\r\nconsole.log(myUrl.search);\r\n<\/pre>\n<p>\u9664\u4e86\u4e0a\u9762\u7684URL\u5730\u5740\u62c6\u5bb6\uff0c<code>URL()<\/code>\u65b9\u6cd5\u8fd8\u6709\u4e00\u4e2a\u9690\u85cf\u7684\u529f\u80fd\uff0c\u5c31\u662f\u53ef\u4ee5\u5b9e\u73b0\u76f8\u5bf9\u5730\u5740\u53d8\u6210\u7edd\u5bf9\u5730\u5740\u7684\u6548\u679c\u3002<\/p>\n<h4>\u8bed\u6cd5<\/h4>\n<pre>var myUrl = new URL(url, [base])<\/pre>\n<p>\u770b\u5230\u540e\u9762\u7684\u53ef\u9009\u53c2\u6570<code>[base]<\/code>\u6ca1\u6709\uff0c\u8fd9\u4e2a\u5c31\u53ef\u4ee5\u5b9e\u73b0\u76f8\u5bf9\u8def\u5f84\u53d8\u6210\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<h4>\u6848\u4f8b<\/h4>\n<pre>var myUrl = new URL(\"\/wordpress\/?p=9227\", \"https:\/\/www.zhangxinxu.com\");\r\nconsole.log(myUrl.href);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u5c31\u662f\u672c\u6587\u7684URL\u5730\u5740\r\n\/\/ https:\/\/www.zhangxinxu.com\/wordpress\/?p=9227<\/span><\/pre>\n<p>\u63a7\u5236\u53f0\u8fd0\u884c\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/202001\/2020-01-17_215447.png\" width=\"528\" height=\"142\" alt=\"URL()\u65b9\u6cd5\u76f8\u5bf9\u5730\u5740\u8f6c\u6362\u6210\u7edd\u5bf9\u5730\u5740\u8fd0\u884c\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u6848\u4f8bagain<\/h4>\n<pre>var myUrl = new URL(\"..\/images\/zhangxinxu.png\", \"https:\/\/images.zhangxinxu.com\/blog\/css\/\");\r\nconsole.log(myUrl.href);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662fhttps:\/\/<wbr>image.zhangxinxu.com<wbr>\/blog<wbr>\/images<wbr>\/zhangxinxu.png<\/span><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/202001\/2020-01-17_215847.png\" width=\"649\" height=\"139\" alt=\"URL\u5730\u5740\u53d8\u5316\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u65b9\u6cd5\u5c01\u88c5<\/h4>\n<p>\u5b9e\u9645\u5f00\u53d1\uff0cbase\u5f80\u5f80\u5c31\u662f\u5f53\u524d\u9875\u9762\u7684\u57df\u540d\u5730\u5740\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5c01\u88c5\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7b80\u5316\u6211\u4eec\u5e73\u65f6\u7684\u8c03\u7528\uff0c\u5982\u4e0b\uff0c\u5927\u5bb6\u53ef\u4ee5\u968f\u610f\u590d\u5236\u7c98\u8d34\u4f7f\u7528\uff0c\u8fde\u7248\u6743\u4fe1\u606f\u90fd\u4e0d\u9700\u8981\u52a0\uff0c\u53ea\u8981\u4e70\u672c\u6211\u5199\u7684\u4e66\u652f\u6301\u4e0b\u5c31\u597d\u4e86\uff1a<\/p>\n<pre><span style=\"color:green;\">\/**\r\n * @description \u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u6210\u7edd\u5bf9\u8def\u5f84\u65b9\u6cd5\r\n * @author zhangxinxu(.com)\r\n*\/<\/span>\r\nvar relative2absolute = function (url, base) {\r\n    if (!base) {\r\n        base = location.protocol + location.host;\r\n    }\r\n    return new URL(url, base).href;\r\n}<\/pre>\n<h4>\u4e2a\u4eba\u70b9\u8bc4<\/h4>\n<p><code>URL()<\/code>\u65b9\u6cd5\u8fdb\u884c\u76f8\u518c\u5730\u5740\u5230\u7edd\u5bf9\u5730\u5740\u8981\u8f6c\u6362\uff0c\u975e\u5e38\u65b9\u4fbf\uff0c\u4e5f\u975e\u5e38\u7075\u6d3b\uff0c\u6ca1\u6709\u4efb\u4f55\u57df\u540d\u7684\u9650\u5236\uff0c\u5c5e\u4e8e\u5404\u79cd\u573a\u666f\u3002<\/p>\n<p>\u4f46\u662f\u6709\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u7f3a\u70b9\uff0c\u5c31\u662f\u517c\u5bb9\u6027\u4e0d\u592a\u597d\u3002Edge 12+\u652f\u6301\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"577\" height=\"393\" alt=\"URL()\u517c\u5bb9\u6027\u622a\u56fe\" class=\"alignnone size-medium\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201908\/2019-08-25_233254.png\"><\/p>\n<p>\u4e0d\u8fc7\u53ef\u4ee5\u6dfb\u52a0polyfill\u4f7f\u652f\u6301\uff0c<a href=\"https:\/\/github.com\/lifaon74\/url-polyfill\" rel=\"noopener nofollow\" target=\"_blank\">https:\/\/github.com\/lifaon74\/url-polyfill<\/a><\/p>\n<p>\u4f46\u662f\u663e\u7136\u5c31\u5f88\u5570\u55e6\u4e86\u3002<\/p>\n<p>\u800c\u4e14Polyfill\u517c\u5bb9\u5230IE9+\uff08\u7edd\u5927\u90e8\u5206\uff09\uff0cIE10+\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u517c\u5bb9\u6027\u65b9\u6cd5\u8db3\u591f\u597d\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u4e0b\u9762\u7684<code>&lt;a&gt;<\/code>\u5143\u7d20\u6cd5\u3002<\/p>\n<h3>\u4e8c\u3001a|form\u5143\u7d20\u6cd5<\/h3>\n<p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u501f\u52a9\u94fe\u63a5\u5143\u7d20\u7684\u76f4\u63a5\u8fd4\u56de\u7edd\u5bf9\u5730\u5740\u7684\u7279\u6027\u5b9e\u73b0\u6211\u4eec\u7684\u9700\u6c42\u3002<\/p>\n<p>\u4f8b\u5982<code>&lt;a&gt;<\/code>\u5143\u7d20\u7684<code>href<\/code>\u5c5e\u6027\uff0c\u6216\u8005<code>&lt;form&gt;<\/code>\u5143\u7d20\u7684<code>action<\/code>\u5c5e\u6027\u3002<\/p>\n<h4>\u6848\u4f8b<\/h4>\n<p>\u76f4\u63a5\u4e0aJavaScript\u4ee3\u7801\uff1a<\/p>\n<pre>var eleLink = document.createElement('a');\r\neleLink.href = \"\/wordpress\/?p=9227\";\r\nconsole.log(eleLink.href);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u5c31\u662f\u672c\u6587\u7684URL\u5730\u5740\r\n\/\/ https:\/\/www.zhangxinxu.com\/wordpress\/?p=9227<\/span><\/pre>\n<p>\u63a7\u5236\u53f0\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/202001\/2020-01-17_224007.png\" width=\"377\" height=\"154\" alt=\"\u501f\u52a9\u94fe\u63a5\u5143\u7d20\u8ba9\u76f8\u5bf9\u5730\u5740\u53d8\u6210\u7edd\u5bf9\u5b9a\u4f4d\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u6848\u4f8b x2<\/h4>\n<p>\u5e26\u6709\u8def\u5f84\u5173\u7cfb\u7684\u5730\u5740\u4e5f\u662f\u7c7b\u4f3c\u7684\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>var eleLink = document.createElement('a');\r\neleLink.href = \"https:\/\/images.zhangxinxu.com\/blog\/css\/\" + \"..\/images\/zhangxinxu.png\";\r\nconsole.log(eleLink.href);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662fhttps:\/\/<wbr>image.zhangxinxu.com<wbr>\/blog<wbr>\/images<wbr>\/zhangxinxu.png<\/span><\/pre>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0cChrome\u6d4f\u89c8\u5668\u63a7\u5236\u53f0console\u6d4b\u8bd5\u7ed3\u679c\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/202001\/2020-01-17_224349.png\" width=\"620\" height=\"164\" alt=\"\u76f8\u5bf9\u5730\u5740\u53d8\u6210\u7edd\u5bf9\u5b9a\u4f4d\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u65b9\u6cd5\u5c01\u88c5<\/h4>\n<p>\u540c\u6837\u7684\u6211\u4eec\u53ef\u4ee5\u5c01\u88c5\u4e00\u4e2a\u975e\u5e38\u4fbf\u6377\u7684\u8def\u5f84\u8f6c\u6362\u65b9\u6cd5\u3002\u5927\u5bb6\u53ef\u4ee5\u968f\u610f\u590d\u5236\u7c98\u8d34\u4f7f\u7528\uff0c\u4f5c\u8005\u4fe1\u606f\u90fd\u53ef\u4ee5\u4e0d\u9700\u8981\u52a0\uff0c\u53ea\u8981\u4e70\u672c\u6211\u5199\u7684\u4e66\u652f\u6301\u4e0b\u5c31\u597d\u4e86\uff1a<\/p>\n<pre><span style=\"color:green;\">\/**\r\n * @description \u76f8\u5bf9\u5730\u5740\u7edd\u5bf9\u5730\u5740\u8f6c\u6362\u65b9\u6cd5\r\n * @author zhangxinxu(.com)\r\n*\/<\/span>\r\nvar relative2absolute = function (url, base) {\r\n    var ele = document.createElement('a');\r\n    ele.href = (base || '') + url;\r\n    return eleLink.href;\r\n}<\/pre>\n<h4>\u4e2a\u4eba\u70b9\u8bc4<\/h4>\n<p>\u501f\u52a9\u94fe\u63a5\u5143\u7d20\u7684\u76f8\u5bf9\u5730\u5740\u8f6c\u6362\u65b9\u6cd5\uff0c\u5176\u6700\u5927\u7684\u4f18\u70b9\u5c31\u662f\u517c\u5bb9\u6027\u975e\u5e38\u597d\uff0c\u5373\u4f7f\u662f\u6781\u4f4e\u7248\u672c\u7684IE\u6d4f\u89c8\u5668\u4e5f\u662f\u652f\u6301\u7684\uff0c\u4f8b\u5982\u4e0b\u9762\u5728IE8\u6d4f\u89c8\u5668\u4e0b\u8dd1\u7684\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/202001\/2020-01-17_224459.png\" width=\"617\" height=\"283\" alt=\"IE8\u6d4f\u89c8\u5668\u4e0b\u8f6c\u7edd\u5bf9\u5730\u5740\u6548\u679c\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u81f3\u4e8e\u7f3a\u70b9\uff0c\u5c31\u662f\u9700\u8981\u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\u624d\u884c\u3002\u5728Node.js\u73af\u5883\u4e0b\u5c31\u7109\u4e86\u3002<\/p>\n<h3>\u4e09\u3001\u5c0f\u5e74\u591c\u5c0f\u7ed3<\/h3>\n<p>\u4eca\u5929\u5c0f\u5e74\u591c\uff0c\u4e2d\u5348\u98df\u5802\u5403\u4e8610\u4e2a\u9984\u9968+\u8377\u5305\u86cb\uff08\u6ca1\u6709\u62cd\u7167\uff09\uff0c\u665a\u4e0a\u5219\u662f\u6574\u4e2a\u524d\u7aef\u7ec4\u56e2\u5efa\u5dee\u4e0d\u591a\u5c06\u8fd130\u4e2a\u4eba\uff0c\u53bb\u635e\u738b\u5403\u4e86\u706b\u9505\uff0c\u8fd9\u4e2a\u6709\u7167\u7247\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/202001\/20200117230205.jpg\" width=\"480\" height=\"360\" alt=\"\u635e\u738b\u706b\u9505\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u7136\u540e\u5199\u4e86\u8fd9\u7bc7\u6587\u7ae0\u3002<\/p>\n<p>\u771f\u662f\u5145\u5b9e\u6ee1\u6ee1\u7684\u4e00\u5929\u3002<\/p>\n<p>\u5bf9\u4e86\uff0c\u5bf9\u4e8e\u672c\u6587\u63d0\u5230\u7684URL()\u65b9\u6cd5\uff0c\u66f4\u8be6\u7ec6\u7684\u5185\u5bb9\u53ef\u4ee5\u53c2\u8003\u6211\u4e4b\u524d\u8fd9\u7bc7\u6587\u7ae0\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8907\">JS URL()\u548cURLSearchParams() API\u63a5\u53e3\u8be6\u7ec6\u4ecb\u7ecd<\/a>\u201d\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f60c.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9227\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9227<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/202001\/url-rel-abs.jpg\" width=\"160\" height=\"100\" alt=\"\u6587\u7ae0\u5934\u56fe\" class=\"alignright size-medium\" \/><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e24\u79cd\u628a\u76f8\u5bf9\u8def\u5f84\u53d8\u6210\u7edd\u5bf9\u8def\u5f84\u7684JS\u65b9\u6cd5\uff0c\u90fd\u975e\u5e38\u7b80\u5355\u5feb\u6377\uff0c\u8fd8\u6709\u5176\u4ed6\u4f18\u7f3a\u70b9\u5927\u5bb6\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[276,1500,13,1273,1413,1099,1411,1548,446,1546,1544,1547,1545],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9227"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=9227"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9227\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}