URL()
ã³ã³ã¹ãã©ã¯ã¿ã¼ãã¾ã㯠URL.parse()
éçã¡ã½ãã (URL API) ã使ç¨ããã¨ãç¸å¯¾åç
§ã¨ãã¼ã¹ URL ã絶対 URL ã«è§£æ±ºãããã¨ãã§ãã¾ãã
ã©ã¡ãã®ã¡ã½ããããæå¤§ 2 ã¤ã®æåå弿°ãåãã絶対 URL ã表ã URL()
ãªãã¸ã§ã¯ããè¿ãã¾ãã æåã®å¼æ°ã¯çµ¶å¯¾ URL ã¾ã㯠URL ã¸ã®ç¸å¯¾åç
§ã表ãã2 çªç®ã®å¼æ°ã¯ãæåã®å¼æ°ã«ç¸å¯¾åç
§ãæå®ããã¦ããå ´åã«ãã®ç¸å¯¾åç
§ã解決ããããã«ä½¿ç¨ããããã¼ã¹ URL ã§ãã ãããã®ã¡ã½ããã¯ãç¡å¹ãª URL ãæ¸¡ããã㨠URL()
ã³ã³ã¹ãã©ã¯ã¿ã¼ãã¨ã©ã¼ãçºçãããã®ã«å¯¾ããparse()
㯠null
ãè¿ãã¨ããç¹ãé¤ãã¦ãåãæ¹æ³ã§ç¸å¯¾åç
§ã解決ãã¾ãã
ä¸è¨ã³ã¼ãã¯ãåã url
ããã³ base
URL å¤ã使ç¨ãã¦ã¡ã½ãããã©ã®ããã«ä½¿ç¨ããããã示ãã¦ãã¾ãã
const url = "articles";
const base = "https://developer.mozilla.org/some/path";
const constructorResult = new URL(url, base);
// => https://developer.mozilla.org/some/articles
const parseResult = URL.parse(url, base);
// => https://developer.mozilla.org/some/articles
ä¾ãã°ãæå®ããããã¼ã¹ URL ã¨ç¸å¯¾åç
§ãã URL
ã解決ãããã¨ã¯ãåç´ã«æå®ããã弿°ãçµåããã ãã§ã¯ãªããã¨ãåããã¾ãã
ãã®å ´åãã«ã¬ã³ããã£ã¬ã¯ããªã¼ã«ç¸å¯¾ãããã¹ (articles
) ãæ¸¡ããã¾ãã ãã¼ã¹ URL ã®ã«ã¬ã³ããã£ã¬ã¯ããªã¼ã¯ãæå¾ã®ã¹ã©ãã·ã¥ã¾ã§ã® URL æååã§ãã ãã®ä¾ã§ã¯ãhttps://developer.mozilla.org/some/path
ã®æ«å°¾ã«ã¹ã©ãã·ã¥ãæå®ããã¦ããªããããã«ã¬ã³ããã£ã¬ã¯ããªã¼ã¯ https://developer.mozilla.org/some/
ã¨ãªããæçµç㪠URL 㯠https://developer.mozilla.org/some/articles
ã¨ãªãã¾ãã
ç¸å¯¾åç
§ã¯ãã«ã¬ã³ããã£ã¬ã¯ããªã¼ (./
)ãã«ã¬ã³ããã£ã¬ã¯ããªã¼ã®è¦ªãã£ã¬ã¯ããªã¼ (../
)ãã¾ãã¯ãµã¤ãã«ã¼ã (/
) ã«å¯¾ããç¸å¯¾ãã¹åç
§ã使ç¨ãã¦ããã¼ã¹ URL ã«å¯¾ãã¦è§£æ±ºããã¾ãã 以ä¸ã®ç« ã§ã¯ãç¸å¯¾ãã¹ã®åã¿ã¤ãã«ã¤ãã¦ã解決ã®ä½æ¥æ¹æ³ã表示ããã¾ãã
./
ã§å§ã¾ãç¸å¯¾åç
§ãã¾ãã¯æ¥é è¾ã®ãªãåç
§ãä¾ãã° ./article
ãarticle
ã./article/
ã¯ãbase
弿°ã§è¡¨ããã URL ã®ã«ã¬ã³ããã£ã¬ã¯ããªã¼ã«å¯¾ããç¸å¯¾åç
§ã§ãã
#log {
height: 90px;
padding: 0.5rem;
border: 1px solid black;
}
const logElement = document.getElementById("log");
function log(text) {
logElement.innerText += `${text}\n`;
}
ãã¼ã¹ URL ã®ã«ã¬ã³ããã£ã¬ã¯ããªã¼ã¯ãæå¾ã®ã¹ã©ãã·ã¥ã¾ã§ã® URL æååã§ãããä¸è¨ã³ã¼ããããã¯ã®ãã¼ã¹æååã©ã¡ãã https://test.example.org/api/
ã§ãã ã«ã¬ã³ããã£ã¬ã¯ããªã¼ããç¸å¯¾åç
§ãã article
ãããã«è¿½å ãããhttps://test.example.org/api/article
ã«è§£æ±ºããã¾ãã
log(new URL("./article", "https://test.example.org/api/").href);
// => https://test.example.org/api/article
log(new URL("article", "https://test.example.org/api/v1").href);
// => https://test.example.org/api/article
åæ§ã«ãä¸è¨ã©ã¡ãã®ãã¼ã¹ URL æååããã«ã¬ã³ã¨ãã£ã¬ã¯ããªã¼ã¯ https://test.example.org/api/v2/
ã§ãã æçµç㪠URL ã解決ããããã«ããããã« story/
㨠story
ã追å ãã¾ãã
log(new URL("./story/", "https://test.example.org/api/v2/").href);
// => https://test.example.org/api/v2/story/
log(new URL("./story", "https://test.example.org/api/v2/v3").href);
// => https://test.example.org/api/v2/story
親ãã£ã¬ã¯ããªã¼ããã®ç¸å¯¾
../
ã¨ããæ¥é è¾ãä»ããç¸å¯¾åç
§ãä¾ãã° ../path
ã¯ãbase
弿°ã§è¡¨ããã URL ã®ç¾å¨ã®ãã£ã¬ã¯ããªã¼ã®è¦ªãã£ã¬ã¯ããªã¼ã«å¯¾ããç¸å¯¾åç
§ã§ãã ../
ã®åã¤ã³ã¹ã¿ã³ã¹ã¯ç¾å¨ã®ãã£ã¬ã¯ããªã¼ãããã©ã«ãã¼å
ãåé¤ãã../
以éã®ããã¹ãã¯æ®ãã®ãã¼ã¹ãã¹ã«è¿½å ããã¾ãã ../
ãè¤æ°åæå®ãããã¨ã§è¦ªãã£ã¬ã¯ããªã¼ã«ç§»åã§ãã¾ããããµã¤ãã«ã¼ãã®ã¬ãã«ã¾ã§ã§ãã
ä¾ãã°ããã¼ã¹ URL https://test.example.com/test/api/v1/
ã¨ãã®è¦ªã®ç¸å¯¾ URL ../some/path
ãæå®ãããå ´åãã«ã¬ã³ããã£ã¬ã¯ããªã¼ã¯ https://test.example.com/test/api/v1/
ã親ãã£ã¬ã¯ããªã¼ã¯ https://test.example.com/test/api/
ã解決ããã絶対 URL 㯠https://test.example.com/test/api/some/path
ã¨ãªãã¾ãã
#log {
height: 80px;
padding: 0.5rem;
border: 1px solid black;
}
const logElement = document.getElementById("log");
function log(text) {
logElement.innerText += `${text}\n`;
}
次ã®ä¾ã§ã¯ããã®ç¹ããã詳ãã説æãã¾ãã ãã¹ã¦ã®ã±ã¼ã¹ã«ããã¦ãã«ã¬ã³ããã£ã¬ã¯ããªã¼ã¯ https://test.example.org/api/v1/v2/
ã§ãï¼2 ã¤ç®ã®ã±ã¼ã¹ã§ã¯ãv3
ãæå¾ã®ã¹ã©ãã·ã¥ã®å¾ã«ç¶ãã¾ãï¼ãç¸å¯¾åç
§ã¯ããããç°ãªã親ãã£ã¬ã¯ããªã¼ãæãã¾ãã
log(new URL("../path", "https://test.example.org/api/v1/v2/").href);
// => https://test.example.org/api/v1/path
log(new URL("../../path", "https://test.example.org/api/v1/v2/v3").href);
// => https://test.example.org/api/path
log(new URL("../../../../path", "https://test.example.org/api/v1/v2/").href);
// => https://test.example.org/path
ã«ã¼ãããã®ç¸å¯¾
/
æ¥é è¾ä»ãã®ç¸å¯¾åç
§ããã¨ãã° /path
ã¯ãbase
弿°ã§æå®ããã URL ã®ãµã¤ãã«ã¼ãã«å¯¾ããç¸å¯¾ãã¹ã¨ãªãã¾ãã ä¾ãã°ããã¼ã¹ URL ã https://test.example.com/api/v1
ã®å ´åãã«ã¼ãç¸å¯¾ URL /some/path
ã®è§£æ±ºå¾ã® URL 㯠https://test.example.com/some/path
ã¨ãªãã¾ãã
ã¡ã¢: ã«ã¼ãç¸å¯¾ URL ã解決ããã¨ãbase
URL ã®ãã¹é¨åã¯é¢ä¿ãªããªãã¾ãã
#log {
height: 80px;
padding: 0.5rem;
border: 1px solid black;
}
const logElement = document.getElementById("log");
function log(text) {
logElement.innerText += `${text}\n`;
}
以ä¸ã«ãããã¤ãä¾ãæãã¾ãã
log(new URL("/some/path", "https://test.example.org/api/").href);
// => https://test.example.org/some/path
log(new URL("/", "https://test.example.org/api/v1/").href);
// => https://test.example.org/
log(new URL("/article", "https://example.com/api/v1/").href);
// => https://example.com/article
é¢é£æ
å ±
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4