URL()
The URL() constructor returns a newly created
URL object representing the URL defined by the parameters.
If the given base URL or the resulting URL are not valid URLs, the JavaScript
TypeError exception is thrown.
Syntax
new URL(url)
new URL(url, base)
Parameters
url- : A string or any other object with a
toString()method. Ifurlis a relative URL,baseis required, and will be used as the base URL. Ifurlis an absolute URL, a givenbasewill be ignored.
- : A string or any other object with a
baseoptional- : A string representing the base URL to use in cases where
urlis a relative URL. If not specified, it defaults toundefined.
- : A string representing the base URL to use in cases where
Note: The
urlandbasearguments will each be stringified from whatever value you pass, just like with other Web APIs that accept a string. In particular, you can use an existingURLobject for either argument, and it will stringify to the object'sURL.href", "hrefproperty.