This class represents a URL with elements such as the scheme, host/domain, path, etc. Use the static parse method to populate a new URL object from a URL string. Be aware that URLs with user and password are not supported.
| Name | Type | Description |
|---|---|---|
scheme |
string |
The URL scheme (e.g. "http" or "https" or "mailto") |
host |
string |
The host (or domain) part of the URL |
opt_path |
string |
optional
The path following the host, pointing to a resource |
opt_params |
Object.<string, *> |
optional
The query string parameters of the URL |
opt_port |
number |
optional
The port on the host on which the host listens (if the value is provided as a string, it must be convertible to an integer) |
opt_anchor |
string |
optional
An optional anchor part of the URL (usually preceded by '#'); |
Throws:
-
Throws an error if either the 'scheme' and 'host' are undefined or if 'opt_params' was specified, but cannot be processed, or if the port is defined, but cannot be converted to a valid port number
Classes
Methods
-
H.service.Url.parse (url, opt_baseURL)H.service.Url static
-
This method parses a URL string and returns a
Urlobject. The URL string must contain at least a scheme and a host.Name Type Description urlstring The URL string to parse
opt_baseURLstring optional The base URL to use to resolve relative URLs. If omitted, the method uses the base URL of the document which loaded the API
Returns:
Type Description H.service.Url The parsed URL object -
addSubDomain (subDomain)H.service.Url
-
This method adds a subdomain to the host in the given
Urlobject.Name Type Description subDomainstring The sub domain (a non-empty string) to be added
Throws:
-
Throws an error if the argument is not a non-empty string
Returns:
Type Description H.service.Url An object representing the given Urlobject after modifications applied by the method -
-
addSubPath (subPath)H.service.Url
-
This method adds a sub-path to the given
Url's path.Name Type Description subPathstring The path to be added
Throws:
-
Throws an error if the argument is not a non-empty string
Returns:
Type Description H.service.Url An object representing the given Urlobject after modifications applied by the method -
-
clone ()H.service.Url
-
This method clones the given URL object. Optionally, mutations can be passed to this method to modify properties of the cloned object. Note that URL parameters are not replaced but merged with the parameters of the given instance.
Returns:
Type Description H.service.Url An object representing the clone of the given URL object -
This method retrieves the anchor from the given
Urlobject.Returns:
Type Description string | undefined The retrieved anchor -
This method retrieves the host name from the given
Urlobject.Returns:
Type Description string A string containing the host (for example 'api.here.com') -
This method retrieves the path part of the given
Urlobject.Returns:
Type Description string | undefined A string containing the retrieved path (for example 'myresources/resource.html') or undefinedif the path is not set -
This method retrieves the query object of the given
Urlobject.Returns:
Type Description Object.<string, *> The retrieved query object -
This method retrieves the scheme for the given
Urlobject.Returns:
Type Description string The scheme (for example 'http') -
This method retrieves a Boolean value indicating whether there are any query string parameter associated with the given
Url.Returns:
Type Description boolean trueif there are parameters,falseif none are present -
mergeQuery (other)H.service.Url
-
This method merges the provided parameters into the given
Url's existing parameters. The key-value pairs which are defined in the argument are used to overwrite the existing URL parameters with matching keys. The key-value pairs which are defined in the argument and are not defined in the givenUrlobject as URL parameters are added. Prototype properties and function properties are not be merged.Name Type Description otherObject.<string, *> The parameters to be merged into the existing query string parameters
Throws:
-
Throws an error if the argument is not of type 'object'
Returns:
Type Description H.service.Url An object representing the given Urlobject after modifications applied by the methodExample
var url = new H.service.Url('http', 'api.here.com'); url.setQuery({ 'foo': 'bar' }); url.mergeQuery({ 'foo': 'baz', 'bar': 'foo' }); var newQuery = url.getQuery(); newQuery['foo'] === 'bar' // false newQuery['foo'] === 'baz' // true newQuery['bar'] === 'foo' // true -
-
setAnchor (anchor)H.service.Url
-
This method sets the anchor for the given
Urlobject.Name Type Description anchorstring | undefined The new anchor or undefined to clear the anchor
Returns:
Type Description H.service.Url The given Urlobject reflecting the modification applied by this method -
setHost (host)H.service.Url
-
This method sets the host for the given
Urlobject.Name Type Description hoststring The new host
Throws:
-
Throws an error if the host is not a string, empty or if the host name contains illegal characters (i.e. starts with '-' or '.' or ends with '.').
Returns:
Type Description H.service.Url An object representing the given Urlobject after modification -
-
setPath (path)H.service.Url
-
This method sets the path for the given
Urlobject.Name Type Description pathstring | undefined A string containing the new path or
undefinedto clear the pathReturns:
Type Description H.service.Url An object representing the given Urlobject after modification -
setQuery (params)H.service.Url
-
This method sets the specified parameters for the given
Urlobject. Keys in this object, which are associated with undefined values, are treated as query string parameters with no value.Name Type Description params!Object.<string, *> | undefined A hash of query string parameters specifying the parameters to be set or a undefined to clear the parameters
Throws:
-
Throws an error if the argument is not an object
Returns:
Type Description H.service.Url The given Urlobject reflecting the modification applied by the method -
-
setScheme (scheme)H.service.Url
-
This method sets the scheme of the given URL object.
Name Type Description schemestring The new scheme
Throws:
-
Throws an error if the scheme is not a string or empty
Returns:
Type Description H.service.Url An object representing the given instance of Urlafter modification -
-
This method retrieves a string representation of the given
Urlobject.Returns:
Type Description string A string representation of the given Urlinstance