Skip to main content
Version: 1.7.0

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. If url is a relative URL, base is required, and will be used as the base URL. If url is an absolute URL, a given base will be ignored.
  • base optional
    • : A string representing the base URL to use in cases where url is a relative URL. If not specified, it defaults to undefined.

Note: The url and base 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 existing URL object for either argument, and it will stringify to the object's URL.href", "href property.