{"id":306852,"date":"2020-07-13T15:00:38","date_gmt":"2020-07-13T15:00:38","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=306852"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=306852","title":{"rendered":"\u041a\u0430\u043a \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c URL \u0432 JavaScript?"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/510742\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tm\/ur\/w4\/tmurw4ra25pamdznkw-qbbldxl8.png\"><\/p>\n<p>  \u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a, \u0434\u0440\u0443\u0437\u044c\u044f! <\/p>\n<p>  \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0412\u0430\u0448\u0435\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0437\u0430\u043c\u0435\u0442\u043a\u0438 <a href=\"https:\/\/dmitripavlutin.com\/parse-url-javascript\/\" rel=\"nofollow\">\u00abHow to Parse URL in JavaScript: hostname, pathname, query, hash\u00bb<\/a> \u0430\u0432\u0442\u043e\u0440\u0430 Dmitri Pavlutin. <\/p>\n<p>  \u0423\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u0438\u043b\u0438, \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u043d\u043e, URL \u2014 \u044d\u0442\u043e \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0432\u0435\u0431-\u0440\u0435\u0441\u0443\u0440\u0441 (\u0432\u0435\u0431-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443, \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0444\u0430\u0439\u043b). URL \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043c\u0435\u0441\u0442\u043e\u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u2014 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b (http, ftp, mailto). <\/p>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043e\u0442 URL \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438:<\/p>\n<pre><code class=\"javascript\">https:\/\/dmitripavlutin.com\/parse-url-javascript <\/code><\/pre>\n<p>  \u0427\u0430\u0441\u0442\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b URL. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0445\u043e\u0441\u0442\u0430 (hostname, <code>dmitripavlutin.com<\/code>) \u0438\u043b\u0438 \u043f\u0443\u0442\u044c (pathname, <code>\/parse-url-javascript<\/code>). <\/p>\n<p>  \u0423\u0434\u043e\u0431\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b URL \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>URL()<\/code>. <\/p>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u0445 URL. <br \/>  <a name=\"habracut\"><\/a><br \/>  <font color=\"#00BFFF\"><\/p>\n<h3>1. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 URL<\/h3>\n<p><\/font><br \/>  \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043b\u0443\u0447\u0448\u0435 \u0442\u044b\u0441\u044f\u0447\u0438 \u0441\u043b\u043e\u0432. \u041d\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0438\u0434\u0435\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b URL: <\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/0i\/dq\/vr\/0idqvryklxfkavmwck0qo4jeksw.png\"><\/p>\n<p>  <font color=\"#00BFFF\"><\/p>\n<h3>2. \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 URL()<\/h3>\n<p><\/font><br \/>  \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>URL()<\/code> \u2014 \u044d\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0430\u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c (\u043f\u0430\u0440\u0441\u0438\u0442\u044c) \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b URL:<\/p>\n<pre><code class=\"javascript\">const url = new URL(relativeOrAbsolute [, absoluteBase]) <\/code><\/pre>\n<p>  \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <code>relativeOrAbsolute<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u043c \u0438\u043b\u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c URL. \u0415\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u2014 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0441\u0441\u044b\u043b\u043a\u0430, \u0442\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, <code>absoluteBase<\/code>, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 URL \u2014 \u043e\u0441\u043d\u043e\u0432\u0443 \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430. <\/p>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c <code>URL()<\/code> \u0441 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u043c URL: <\/p>\n<pre><code class=\"javascript\">const url = new URL('http:\/\/example.com\/path\/index.html')  url.href \/\/ 'http:\/\/example.com\/path\/index.html' <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u0443\u0435\u043c \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0438 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 URL: <\/p>\n<pre><code class=\"javascript\">const url = new URL('\/path\/index.html', 'http:\/\/example.com')  url.href \/\/ 'http:\/\/example.com\/path\/index.html' <\/code><\/pre>\n<p>  \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>href<\/code> \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 <code>URL()<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e URL-\u0441\u0442\u0440\u043e\u043a\u0443. <\/p>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 <code>URL()<\/code>, \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c URL. \u0414\u043b\u044f \u0441\u043f\u0440\u0430\u0432\u043a\u0438, \u0432\u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 <code>URL()<\/code>:<\/p>\n<pre><code class=\"javascript\">interface URL {   href:     USVString;   protocol: USVString;   username: USVString;   password: USVString;   host:     USVString;   hostname: USVString;   port:     USVString;   pathname: USVString;   search:   USVString;   hash:     USVString;    readonly origin: USVString;   readonly searchParams: URLSearchParams;    toJSON(): USVString; } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0442\u0438\u043f <code>USVString<\/code> \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e JavaScript \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443. <\/p>\n<p>  <font color=\"#00BFFF\"><\/p>\n<h3>3. \u0421\u0442\u0440\u043e\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 (query string)<\/h3>\n<p><\/font><br \/>  \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>url.search<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 URL, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0443\u044e\u0441\u044f \u0441 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u0430 <code>?<\/code>:<\/p>\n<pre><code class=\"javascript\">const url = new URL(     'http:\/\/example.com\/path\/index.html?message=hello&amp;who=world' )  url.search \/\/ '?message=hello&amp;who=world' <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, <code>url.search<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 (&#187;): <\/p>\n<pre><code class=\"javascript\">const url1 = new URL('http:\/\/example.com\/path\/index.html') const url2 = new URL('http:\/\/example.com\/path\/index.html?')  url1.search \/\/ '' url2.search \/\/ '' <\/code><\/pre>\n<p>  <font color=\"#3CB371\"><\/p>\n<h5>3.1. \u0420\u0430\u0437\u0431\u043e\u0440 (\u043f\u0430\u0440\u0441\u0438\u043d\u0433) \u0441\u0442\u0440\u043e\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/h5>\n<p><\/font><br \/>  \u0412\u043c\u0435\u0441\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0435\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b. <\/p>\n<p>  \u041b\u0435\u0433\u043a\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>url.searchParams<\/code>. \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 URLSeachParams. <\/p>\n<p>  \u041e\u0431\u044a\u0435\u043a\u0442 <code>URLSearchParams<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0441\u0442\u0440\u043e\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 (<code>get(param), has(param)<\/code> \u0438 \u0442.\u0434.).<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440: <\/p>\n<pre><code class=\"javascript\">const url = new Url(     'http:\/\/example.com\/path\/index.html?message=hello&amp;who=world' )  url.searchParams.get('message') \/\/ 'hello' url.searchParams.get('missing') \/\/ null <\/code><\/pre>\n<p>  <code>url.searchParams.get('message')<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 <code>message<\/code> \u0441\u0442\u0440\u043e\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. <\/p>\n<p>  \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0443 <code>url.searchParams.get('missing')<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>null<\/code>. <\/p>\n<p>  <font color=\"#00BFFF\"><\/p>\n<h3>4. \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0445\u043e\u0441\u0442\u0430 (hostname)<\/h3>\n<p><\/font><br \/>  \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <code>url.hostname<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0445\u043e\u0441\u0442\u0430 URL: <\/p>\n<pre><code class=\"javascript\">const url = new URL('http:\/\/example.com\/path\/index.html')  url.hostname \/\/ 'example.com' <\/code><\/pre>\n<p>  <font color=\"#00BFFF\"><\/p>\n<h3>5. \u041f\u0443\u0442\u044c (pathname)<\/h3>\n<p><\/font><br \/>  \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>url.pathname<\/code> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u0443\u0442\u044c URL: <\/p>\n<pre><code class=\"javascript\">const url = new URL('http:\/\/example.com\/path\/index.html?param=value')  url.pathname \/\/ '\/path\/index.html' <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 URL \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043f\u0443\u0442\u0438, <code>url.pathname<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0438\u043c\u0432\u043e\u043b <code>\/<\/code>: <\/p>\n<pre><code class=\"javascript\">const url = new URL('http:\/\/example.com\/');  url.pathname; \/\/ '\/' <\/code><\/pre>\n<p>  <font color=\"#00BFFF\"><\/p>\n<h3>6. \u0425\u0435\u0448 (hash)<\/h3>\n<p><\/font><br \/>  \u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0445\u0435\u0448 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0447\u0435\u0440\u0435\u0437 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>url.hash<\/code>: <\/p>\n<pre><code class=\"javascript\">const url = new URL('http:\/\/example.com\/path\/index.html#bottom')  url.hash \/\/ '#bottom' <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0445\u0435\u0448 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, <code>url.hash<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 (&#187;): <\/p>\n<pre><code class=\"javascript\">const url = new URL('http:\/\/example.com\/path\/index.html')  url.hash \/\/ '' <\/code><\/pre>\n<p>  <font color=\"#00BFFF\"><\/p>\n<h3>7. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 (\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f) URL<\/h3>\n<p><\/font><br \/>  \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 <code>new URL()<\/code> \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440, \u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e URL. \u0415\u0441\u043b\u0438 URL \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0430\u043b\u0438\u0434\u043d\u044b\u043c, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>TypeError<\/code>. <\/p>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>http :\/\/example.com<\/code> \u043d\u0435 \u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0439 URL, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u043e\u0441\u043b\u0435 <code>http<\/code> \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0431\u0435\u043b. <\/p>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 URL: <\/p>\n<pre><code class=\"javascript\">try {     const url = new URL('http :\/\/example.com') } catch (error) {     error \/\/ TypeError, &quot;Failed to construct URL: Invalid URL&quot; } <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 <code>'http :\/\/example.com'<\/code> \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 URL, \u043a\u0430\u043a \u0438 \u043e\u0436\u0438\u0434\u0430\u043b\u043e\u0441\u044c, <code>new URL('http :\/\/example.com')<\/code> \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 <code>TypeError<\/code>. <\/p>\n<p>  <font color=\"#00BFFF\"><\/p>\n<h3>8. \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 URL<\/h3>\n<p><\/font><br \/>  \u0422\u0430\u043a\u0438\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430, \u043a\u0430\u043a <code>search, hostname, pathname, hash<\/code> \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438. <\/p>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0445\u043e\u0441\u0442\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e URL \u0441 <code>red.com<\/code> \u043d\u0430 <code>blue.io<\/code>: <\/p>\n<pre><code class=\"javascript\">const url = new URL('http:\/\/red.com\/path\/index.html')  url.href \/\/ 'http:\/\/red.com\/path\/index.html'  url.hostname = 'blue.io'  url.href \/\/ 'http:\/\/blue.io\/path\/index.html' <\/code><\/pre>\n<p>  \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <code>origin, searchParams<\/code> \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f.<\/p>\n<p>  <font color=\"#00BFFF\"><\/p>\n<h3>9. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p><\/font><br \/>  \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>URL()<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0440\u0430\u0437\u0431\u043e\u0440\u0430 (\u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430) \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 (\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438) URL \u0432 JavaScript. <\/p>\n<p>  <code>new URL(relativeOrAbsolute, [, absoluteBase]<\/code> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 \u0438\u043b\u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 URL. \u0415\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c URL, \u0432\u0442\u043e\u0440\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 URL \u2014 \u043e\u0441\u043d\u043e\u0432\u0430 \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430. <\/p>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 <code>URL()<\/code>, \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c URL: <\/p>\n<ul>\n<li><code>url.search<\/code> \u2014 \u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/li>\n<li><code>url.searchParams<\/code> \u2014 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 <code>URLSearchParams<\/code> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0441\u0442\u0440\u043e\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/li>\n<li><code>url.hostname<\/code> \u2014 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0445\u043e\u0441\u0442\u0430<\/li>\n<li><code>url.pathname<\/code> \u2014 \u043f\u0443\u0442\u044c<\/li>\n<li><code>url.hash<\/code> \u2014 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0435\u0448\u0430<\/li>\n<\/ul>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/510742\/\"> https:\/\/habr.com\/ru\/post\/510742\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/510742\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tm\/ur\/w4\/tmurw4ra25pamdznkw-qbbldxl8.png\"><\/p>\n<p>  \u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a, \u0434\u0440\u0443\u0437\u044c\u044f! <\/p>\n<p>  \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0412\u0430\u0448\u0435\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0437\u0430\u043c\u0435\u0442\u043a\u0438 <a href=\"https:\/\/dmitripavlutin.com\/parse-url-javascript\/\" rel=\"nofollow\">\u00abHow to Parse URL in JavaScript: hostname, pathname, query, hash\u00bb<\/a> \u0430\u0432\u0442\u043e\u0440\u0430 Dmitri Pavlutin. <\/p>\n<p>  \u0423\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u0438\u043b\u0438, \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u043d\u043e, URL \u2014 \u044d\u0442\u043e \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0432\u0435\u0431-\u0440\u0435\u0441\u0443\u0440\u0441 (\u0432\u0435\u0431-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443, \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0444\u0430\u0439\u043b). URL \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043c\u0435\u0441\u0442\u043e\u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u2014 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b (http, ftp, mailto). <\/p>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043e\u0442 URL \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438:<\/p>\n<pre><code class=\"javascript\">https:\/\/dmitripavlutin.com\/parse-url-javascript <\/code><\/pre>\n<p>  \u0427\u0430\u0441\u0442\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b URL. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0445\u043e\u0441\u0442\u0430 (hostname, <code>dmitripavlutin.com<\/code>) \u0438\u043b\u0438 \u043f\u0443\u0442\u044c (pathname, <code>\/parse-url-javascript<\/code>). <\/p>\n<p>  \u0423\u0434\u043e\u0431\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b URL \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>URL()<\/code>. <\/p>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u0445 URL.   <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-306852","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/306852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=306852"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/306852\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}