Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. URL
  4. parse()

URL: parse() static method

Baseline 2024
Newly available

Since ⁨September 2024⁩, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Note: This feature is available inWeb Workers.

TheURL.parse() static method of theURL interface returns a newly createdURL object representing the URL defined by the parameters.

If the given base URL or the resulting URL are not parsable and valid URLs,null is returned.This is an alternative to using theURL() constructor to construct aURL within atry...catch block, or usingcanParse() to check the parameters and returningnull if the method returnsfalse.

Syntax

js
URL.parse(url)URL.parse(url, base)

Parameters

url

A string or any other object with astringifier that represents an absolute URL or a relative reference to a URL.Ifurl is a relative reference,base is required, and is used to resolve the final URL.Ifurl is an absolute URL, a givenbase will not be used to create the resulting URL.

baseOptional

A string representing the base URL to use in cases whereurl is a relative URL.If not specified, it defaults toundefined.

When you specify abase URL, the resolved URL is not simply a concatenation ofurl andbase.Relative references to the parent and current directory are resolved are relative to the current directory of thebase URL, which includes only path segments up until the last forward-slash, but not any after.Relative references to the root are resolved relative to the base origin.For more information seeResolving relative references to a URL.

Note:Theurl andbase arguments will each be stringified from whatever value you pass, such as anHTMLAnchorElement orHTMLAreaElement element, just like with other Web APIs that accept a string.In particular, you can use an existingURL object for either argument, and it will be stringified from the object'shref property.

Return value

AURL if the parameters can be resolved to a valid URL;null otherwise.

Examples

Resolving relative references to a URL andURL() constructor provide additional examples demonstrating how differenturl andbase values are resolved to a final absolute URL (though primarily usingURL()).

Using URL.parse()

This live example demonstrates how to use theURL.parse() static method for a few different absolute and relative reference values.

<pre></pre>
#log {  height: 100px;  overflow: scroll;  padding: 0.5rem;  border: 1px solid black;}
const logElement = document.getElementById("log");function log(text) {  logElement.innerText += `${text}\n`;}

First we check that theURL.parse() method is supported using the condition"parse" in URL.If the method is supported we log the result of checking an absolute URL, a relative reference and a base URL, a relative reference with a morecomplicated base URL, a valid absolute URL with a valid base URL (which is not used), and an invalid base URL that results in the method returningnull.

We also log the case whenURL.parse() is not supported.

js
if ("parse" in URL) {  // Absolute URL  let result = URL.parse("https://developer.mozilla.org/en-US/docs");  log(`[1]: ${result.href}`);  // Relative reference to a valid base URL  result = URL.parse("en-US/docs", "https://developer.mozilla.org");  log(`[2]: ${result.href}`);  // Relative reference to a "complicated" valid base URL  // (only the scheme and domain are used to resolve url)  result = URL.parse(    "/different/place",    "https://developer.mozilla.org:443/some/path?id=4",  );  log(`[3]: ${result.href}`);  // Absolute url argument (base URL ignored)  result = URL.parse(    "https://example.org/some/docs",    "https://developer.mozilla.org",  );  log(`[4]: ${result.href}`);  // Invalid base URL (missing colon)  result = URL.parse("en-US/docs", "https//developer.mozilla.org");  log(`[5]: ${result}`);} else {  log("URL.parse() not supported");}

Last of all, the code below demonstrates that the arguments don't have to be strings, by passing anURL object for thebase parameter.

js
if ("parse" in URL) {  // Relative reference with base URL supplied as a URL object  result = URL.parse("/en-US/docs", new URL("https://developer.mozilla.org/"));  log(`[6]: ${result.href}`);}

The results of each of the checks are shown below.

Specifications

Specification
URL
# dom-url-parse

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp