Edit this page

up.viewport up.viewport.saveFocus([viewport], [options])
JavaScript function

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

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.

Saved state will be associated with the given layer's location. It can later be restored by calling up.viewport.restoreScroll() at the same location, or by following a link with an [up-focus="restore"] attribute to that same location.

Unpoly automatically saves focus-related state before navigating.


Parameters

[viewport] ElementorArray<Element> optional

The viewports for which to save focus state.

Defaults to all viewports within the given layer.

[options.location] string optional

The URL for which to save focus state.

If omitted, the given layer's location is used.

[options.layer='current'] string optional

The layer for which to save scroll positions.

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