---
name: "up.util.normalizeURL"
area: "API"
kind: "JavaScript function"
parent: "up.util module"
url: "https://unpoly.com/api/up.util.normalizeURL"
---
<nav aria-label="Unpoly docs">[All docs](https://unpoly.com/index.md) · [up.util module](https://unpoly.com/api/up.util.md)</nav>

# up.util.normalizeURL(url, [options])

Returns a normalized version of the given URL string.

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

<nav aria-label="Contents">

#### Contents

- [Comparing normalized URLs](#comparing-normalized-urls)
- [Excluding URL components](#excluding-url-components)
- [Limitations](#limitations)
- [Parameters](#parameters)
- [Return value](#return-value)

</nav>

## Comparing normalized URLs {#comparing-normalized-urls}

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

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

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

```js
up.util.normalizeURL('http://current-host/path') // result: '/path'
up.util.normalizeURL('http://other-host/path') // result: 'http://other-host/path'
```

Relative paths are normalized to absolute paths:

```js
up.util.normalizeURL('index.html') // result: '/path/index.html'
```

## Excluding URL components {#excluding-url-components}

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

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

## Limitations {#limitations}

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

---

## Parameters {#parameters}

### url {#url}

(required)

the URL to normalize

Type: string | [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL)

### [options.host='cross-domain'] {#options.host}

(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 differs 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://`.

Type: boolean

### [options.hash=true] {#options.hash}

(optional)

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

Type: boolean

### [options.search=true] {#options.search}

(optional)

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

Type: boolean

### [options.trailingSlash=true] {#options.trailingSlash}

(optional)

Whether to include a trailing slash from the pathname.

Type: boolean

---

## Return value {#return-value}

The normalized URL.

Type: string

