---
name: "up.viewport.saveFocus"
area: "API"
kind: "JavaScript function"
parent: "up.viewport module"
url: "https://unpoly.com/api/up.viewport.saveFocus"
---
<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.saveFocus([options])

Saves focus-related state for later restoration.

Saved state includes:

- Which element is focused.
- The cursor position within a focused input element.
- The selection range within a focused input element.
- The scroll position within a focused input element.

State can only be preserved if the focused element is [targetable](https://unpoly.com/api/up.fragment.isTargetable.md).

Saved state will be associated with the given layer's location. It can later be restored by calling [`up.viewport.restoreFocus()`](https://unpoly.com/api/up.viewport.restoreFocus.md) at the same location, or by following a link with an [`[up-focus="restore"]`](https://unpoly.com/learn/focus.md#restore) attribute to that same location.

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

---

## Parameters {#parameters}

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

(optional)

The URL for which to save focus state.

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 focus state.

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

Type: string

