Edit this page

up.util up.util.normalizeURL(the, [options])
JavaScript function

This feature is experimental. Please share your experiences so we know what to keep or change.

Returns a normalized version of the given URL string.

Two URLs that point to the same resource should normalize to the same string.

Comparing normalized URLs

The main purpose of this function is to normalize two URLs for string comparison:

up.util.normalizeURL('http://current-host/path') === up.util.normalizeURL('/path') // => true

By default the hostname is only included if it points to a different origin:

up.util.normalizeURL('http://current-host/path') // => '/path'
up.util.normalizeURL('http://other-host/path') // => 'http://other-host/path'

Relative paths are normalized to absolute paths:

up.util.normalizeURL('index.html') // => '/path/index.html'

Excluding URL components

You may pass options to exclude URL components from the normalized string:

up.util.normalizeURL('/foo?query=bar', { query: false }) => '/foo'
up.util.normalizeURL('/bar#hash', { hash: false }) => '/bar'

Limitations

  • Username and password are always omitted from the normalized URL.
  • Only http and https schemes are supported.

Parameters

the stringorURL

URL to normalize

[options.host='cross-domain'] boolean optional

Whether to include protocol, hostname and port in the normalized URL.

When set to 'cross-domain' (the default), the host is only included if it differ's from the page's hostname.

The port is omitted if the port is the standard port for the given protocol, e.g. :443 for https://.

[options.hash=true] boolean optional

Whether to include an #hash anchor in the normalized URL.

[options.search=true] boolean optional

Whether to include a ?query string in the normalized URL.

[options.trailingSlash=true] boolean optional

Whether to include a trailing slash from the pathname.

Return value

string

The normalized URL.