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

# up.viewport.saveScroll([viewport], [options])

Saves scroll positions for later restoration.

The scroll positions will be associated with the current URL. They can later be restored by calling [`up.viewport.restoreScroll()`](https://unpoly.com/api/up.viewport.restoreScroll.md) at the same URL, or by following a link with an [`[up-scroll="restore"]`](https://unpoly.com/learn/scrolling.md#restore) attribute.

Unpoly automatically saves scroll positions before [navigating](https://unpoly.com/learn/navigation-defaults.md).

---

## Parameters {#parameters}

### [viewport] {#viewport}

(optional)

The viewports for which to save scroll positions.

Defaults to all viewports within the given layer.

Type: [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) | [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Element](https://developer.mozilla.org/en-US/docs/Web/API/Element)>

### [options.location] {#options.location}

(optional)

The URL for which to save scroll positions.

If omitted, the given [layer's location](https://unpoly.com/api/up.Layer.prototype.location.md) is used.

Type: string

### [options.layer='current'] {#options.layer}

(optional)

The layer for which to save scroll positions.

If omitted, positions for the current layer will be saved.

Type: string

