Packages

class URL extends URL_ with _PathLike

Browser-compatible URL class, implemented by following the WHATWG URL Standard. [Examples of parsed URLs](https://url.spec.whatwg.org/#example-url-parsing) may be found in the Standard itself. The URL class is also available on the global object.

In accordance with browser conventions, all properties of URL objects are implemented as getters and setters on the class prototype, rather than as data properties on the object itself. Thus, unlike legacy urlObject s, using the delete keyword on any properties of URL objects (e.g. delete myURL.protocol, delete myURL.pathname, etc) has no effect but will still return true.

Annotations
@JSType() @JSImport("node:url", "URL") @native()
Since

v7.0.0, v6.13.0

Linear Supertypes
_PathLike, URL_, StObject, Object, Any, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. URL
  2. _PathLike
  3. URL_
  4. StObject
  5. Object
  6. Any
  7. AnyRef
  8. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new URL(input: String, base: String)
  2. new URL(input: String, base: URL_)
  3. new URL(input: String)
  4. new URL()
    Attributes
    protected

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  8. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  9. def hasOwnProperty(v: String): Boolean
    Definition Classes
    Object
  10. var hash: String

    Gets and sets the fragment portion of the URL.

    Gets and sets the fragment portion of the URL.

    js const myURL = new URL('https://example.org/foo#bar'); console.log(myURL.hash); // Prints #bar

    myURL.hash = 'baz'; console.log(myURL.href); // Prints https://example.org/foo#baz

    Invalid URL characters included in the value assigned to the hash property are percent-encoded. The selection of which characters to percent-encode may vary somewhat from what the parse and format methods would produce.

    Definition Classes
    URL_
  11. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  12. var host: String

    Gets and sets the host portion of the URL.

    Gets and sets the host portion of the URL.

    js const myURL = new URL('https://example.org:81/foo'); console.log(myURL.host); // Prints example.org:81

    myURL.host = 'example.com:82'; console.log(myURL.href); // Prints https://example.com:82/foo

    Invalid host values assigned to the host property are ignored.

    Definition Classes
    URL_
  13. var hostname: String

    Gets and sets the host name portion of the URL.

    Gets and sets the host name portion of the URL. The key difference betweenurl.host and url.hostname is that url.hostname does _not_ include the port.

    js const myURL = new URL('https://example.org:81/foo'); console.log(myURL.hostname); // Prints example.org

    // Setting the hostname does not change the port myURL.hostname = 'example.com:82'; console.log(myURL.href); // Prints https://example.com:81/foo

    // Use myURL.host to change the hostname and port myURL.host = 'example.org:82'; console.log(myURL.href); // Prints https://example.org:82/foo

    Invalid host name values assigned to the hostname property are ignored.

    Definition Classes
    URL_
  14. var href: String

    Gets and sets the serialized URL.

    Gets and sets the serialized URL.

    js const myURL = new URL('https://example.org/foo'); console.log(myURL.href); // Prints https://example.org/foo

    myURL.href = 'https://example.com/bar'; console.log(myURL.href); // Prints https://example.com/bar

    Getting the value of the href property is equivalent to calling toString.

    Setting the value of this property to a new value is equivalent to creating a new URL object using new URL(value). Each of the URLobject's properties will be modified.

    If the value assigned to the href property is not a valid URL, a TypeErrorwill be thrown.

    Definition Classes
    URL_
  15. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  16. def isPrototypeOf(v: Object): Boolean
    Definition Classes
    Object
  17. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  18. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  19. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  20. val origin: String

    Gets the read-only serialization of the URL's origin.

    Gets the read-only serialization of the URL's origin.

    js const myURL = new URL('https://example.org/foo/bar?baz'); console.log(myURL.origin); // Prints https://example.org

    js const idnURL = new URL('https://測試'); console.log(idnURL.origin); // Prints https://xn--g6w251d

    console.log(idnURL.hostname); // Prints xn--g6w251d

    Definition Classes
    URL_
  21. var password: String

    Gets and sets the password portion of the URL.

    Gets and sets the password portion of the URL.

    js const myURL = new URL('https://abc:[email protected]'); console.log(myURL.password); // Prints xyz

    myURL.password = '123'; console.log(myURL.href); // Prints https://abc:[email protected]

    Invalid URL characters included in the value assigned to the password property are percent-encoded. The selection of which characters to percent-encode may vary somewhat from what the parse and format methods would produce.

    Definition Classes
    URL_
  22. var pathname: String

    Gets and sets the path portion of the URL.

    Gets and sets the path portion of the URL.

    js const myURL = new URL('https://example.org/abc/xyz?123'); console.log(myURL.pathname); // Prints /abc/xyz

    myURL.pathname = '/abcdef'; console.log(myURL.href); // Prints https://example.org/abcdef?123

    Invalid URL characters included in the value assigned to the pathnameproperty are percent-encoded. The selection of which characters to percent-encode may vary somewhat from what the parse and format methods would produce.

    Definition Classes
    URL_
  23. var port: String

    Gets and sets the port portion of the URL.

    Gets and sets the port portion of the URL.

    The port value may be a number or a string containing a number in the range0 to 65535 (inclusive). Setting the value to the default port of theURL objects given protocol will result in the port value becoming the empty string ().

    The port value can be an empty string in which case the port depends on the protocol/scheme:

    <omitted>

    Upon assigning a value to the port, the value will first be converted to a string using .toString().

    If that string is invalid but it begins with a number, the leading number is assigned to port. If the number lies outside the range denoted above, it is ignored.

    js const myURL = new URL('https://example.org:8888'); console.log(myURL.port); // Prints 8888

    // Default ports are automatically transformed to the empty string // (HTTPS protocol's default port is 443) myURL.port = '443'; console.log(myURL.port); // Prints the empty string console.log(myURL.href); // Prints https://example.org/

    myURL.port = 1234; console.log(myURL.port); // Prints 1234 console.log(myURL.href); // Prints https://example.org:1234/

    // Completely invalid port strings are ignored myURL.port = 'abcd'; console.log(myURL.port); // Prints 1234

    // Leading numbers are treated as a port number myURL.port = '5678abcd'; console.log(myURL.port); // Prints 5678

    // Non-integers are truncated myURL.port = 1234.5678; console.log(myURL.port); // Prints 1234

    // Out-of-range numbers which are not represented in scientific notation // will be ignored. myURL.port = 1e10; // 10000000000, will be range-checked as described below console.log(myURL.port); // Prints 1234

    Numbers which contain a decimal point, such as floating-point numbers or numbers in scientific notation, are not an exception to this rule. Leading numbers up to the decimal point will be set as the URL's port, assuming they are valid:

    js myURL.port = 4.567e21; console.log(myURL.port); // Prints 4 (because it is the leading number in the string '4.567e21')

    Definition Classes
    URL_
  24. def propertyIsEnumerable(v: String): Boolean
    Definition Classes
    Object
  25. var protocol: String

    Gets and sets the protocol portion of the URL.

    Gets and sets the protocol portion of the URL.

    js const myURL = new URL('https://example.org'); console.log(myURL.protocol); // Prints https:

    myURL.protocol = 'ftp'; console.log(myURL.href); // Prints ftp://example.org/

    Invalid URL protocol values assigned to the protocol property are ignored.

    Definition Classes
    URL_
  26. var search: String

    Gets and sets the serialized query portion of the URL.

    Gets and sets the serialized query portion of the URL.

    js const myURL = new URL('https://example.org/abc?123'); console.log(myURL.search); // Prints ?123

    myURL.search = 'abc=xyz'; console.log(myURL.href); // Prints https://example.org/abc?abc=xyz

    Any invalid URL characters appearing in the value assigned the searchproperty will be percent-encoded. The selection of which characters to percent-encode may vary somewhat from what the parse and format methods would produce.

    Definition Classes
    URL_
  27. val searchParams: URLSearchParams

    Gets the URLSearchParams object representing the query parameters of the URL.

    Gets the URLSearchParams object representing the query parameters of the URL. This property is read-only but the URLSearchParams object it provides can be used to mutate the URL instance; to replace the entirety of query parameters of the URL, use the search setter. See URLSearchParams documentation for details.

    Use care when using .searchParams to modify the URL because, per the WHATWG specification, the URLSearchParams object uses different rules to determine which characters to percent-encode. For instance, the URL object will not percent encode the ASCII tilde (~) character, while URLSearchParams will always encode it:

    js const myUrl = new URL('https://example.org/abc?foo=~bar');

    console.log(myUrl.search); // prints ?foo=~bar

    // Modify the URL via searchParams... myUrl.searchParams.sort();

    console.log(myUrl.search); // prints ?foo=%7Ebar

    Definition Classes
    URL_
  28. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  29. def toJSON(): String

    The toJSON() method on the URL object returns the serialized URL.

    The toJSON() method on the URL object returns the serialized URL. The value returned is equivalent to that of href and toString.

    This method is automatically called when an URL object is serialized with [JSON.stringify()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).

    js const myURLs = [ new URL('https://www.example.com'), new URL('https://test.example.org'), ]; console.log(JSON.stringify(myURLs)); // Prints ["https://www.example.com/","https://test.example.org/"]

    Definition Classes
    URL_
  30. def toLocaleString(): String
    Definition Classes
    Object
  31. def toString(): String
    Definition Classes
    AnyRef → Any
  32. var username: String

    Gets and sets the username portion of the URL.

    Gets and sets the username portion of the URL.

    js const myURL = new URL('https://abc:[email protected]'); console.log(myURL.username); // Prints abc

    myURL.username = '123'; console.log(myURL.href); // Prints https://123:[email protected]/

    Any invalid URL characters appearing in the value assigned the usernameproperty will be percent-encoded. The selection of which characters to percent-encode may vary somewhat from what the parse and format methods would produce.

    Definition Classes
    URL_
  33. def valueOf(): Any
    Definition Classes
    Object
  34. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  35. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  36. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

Inherited from _PathLike

Inherited from URL_

Inherited from StObject

Inherited from Object

Inherited from Any

Inherited from AnyRef

Inherited from Any

Ungrouped