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. Ifurl
is a relative URL,base
is required, and will be used as the base URL. Ifurl
is an absolute URL, a givenbase
will be ignored.
- : A string or any other object with a
base
optional- : A string representing the base URL to use in cases where
url
is a relative URL. If not specified, it defaults toundefined
.
- : A string representing the base URL to use in cases where
Note: The
url
andbase
arguments 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 existingURL
object for either argument, and it will stringify to the object'sURL.href", "href
property.