Edit this page

up.link [up-follow]
HTML selector

Follows this link with JavaScript and updates a fragment with the server response.

Following a link is considered navigation by default.

Example

This will update the fragment <div class="content"> with the same element fetched from /posts/5:

<a href="/posts/5" up-follow up-target=".content">Read post</a>

If no [up-target] attribute is set, the main target is updated.

You can configure Unpoly to follow all links on a page without requiring an [up-follow] attribute.

See Handling all links and forms.

You can tell Unpoly to ignore clicks on an [up-follow] link, causing the link to be non-interactive. Use one of the following methods:

To force a full page load when a followable link is clicked:

You can set an [up-follow] attribute on any non-interactive element to make it behave like a hyperlink:

<span up-follow up-href="/details">Read more</span>

See Acting like a hyperlink for details.

Advanced fragment changes

Links can update multiple fragments or append content to an existing element.

See Fragment placement for details.

Short notation

You may omit the [up-follow] attribute if the link has one of the following attributes:

  • [up-target]
  • [up-layer]
  • [up-transition]
  • [up-content]
  • [up-fragment]
  • [up-document]

Such a link will still be followed through Unpoly.


Modifying attributes

[up-navigate='true'] optional

Whether this fragment update is considered navigation.

Setting this to false will disable most defaults documented below, causing Unpoly to render a fragment without side-effects like updating history or scrolling.

[href] optional

The URL to fetch from the server.

See loading content from a URL.

To use a different URL when a link is followed through Unpoly (as opposed to a browser's full page load), set an [up-href] attribute.

Instead of making a server request, you may also render an existing string of HTML.

[up-target] optional

The target selector to update.

If omitted a main target will be rendered.

[up-fallback='true'] optional

Specifies behavior if the target selector is missing from the current page or the server response.

If set to a CSS selector, Unpoly will attempt to replace that selector instead.

If set to true Unpoly will attempt to replace a main target instead.

If set to false Unpoly will immediately reject the render promise.

[up-match='region'] optional

Controls which fragment to update when the [up-target] selector yields multiple results.

When set to 'region' Unpoly will prefer to update fragments in the region of the origin element.

If set to 'first' Unpoly will always update the first matching fragment.

Defaults to up.fragment.config.match, which defaults to 'region'.

[up-method='get'] optional

The HTTP method to use for the request.

Common values are get, post, put, patch and delete. The value is case insensitive.

The HTTP method may also be passed as an [data-method] attribute.

By default, methods other than get or post will be converted into a post request, and carry their original method as a configurable _method parameter.

[up-params] optional

A JSON object with additional parameters that should be sent as the request's query string or payload.

When making a GET request to a URL with a query string, the given { params } will be added to the query parameters.

[up-headers] optional

A JSON object with additional request headers.

Unpoly will by default send a number of custom request headers. E.g. the X-Up-Target header includes the target selector. See up.protocol for details.

[up-content] optional

The new inner HTML for the targeted fragment.

See Updating an element's inner HTML from a string.

[up-fragment] optional

A string of HTML comprising only the new fragment's outer HTML.

With an [up-fragment] attribute you can omit the [up-target] attribute. The target will be derived from the root element in the given HTML.

See Rendering a string that only contains the fragment.

[up-document] optional

A string of HTML containing the targeted fragment.

See Extracting an element's outer HTML from a larger HTML string.

[up-fail] optional

Whether the server response should be considered failed.

For failed responses Unpoly will use attributes prefixed with up-fail, e.g. [up-fail-target]. See handling server errors for details.

By default any HTTP status code other than 2xx or 304 is considered an error code. Set [up-fail=false] to handle any response as successful, even with a 4xx or 5xx status code.

[up-history='auto'] optional

Whether the browser URL, window title and meta tags will be updated.

If set to true, the history will always be updated, using the title and URL from the server response, or from given [up-title] and [up-location] attributes.

If set to auto history will be updated if the [up-target] matches a selector in up.fragment.config.autoHistoryTargets. By default this contains all main targets.

If set to false, the history will remain unchanged.

See Updating history.

[up-title] optional

An explicit document title to set before rendering.

By default the title is extracted from the response's <title> tag. To prevent the title from being updated, set [up-title=false] to explicitly

This attribute is only used when updating history.

[up-location] optional

An explicit URL to set before rendering.

By default Unpoly will use the link's [href] or the final URL after the server redirected. To prevent the URL from being updated, set [up-location=false].

This attribute is only used when updating history.

[up-meta-tags] optional

Whether to update meta tags in the <head>.

By default Unpoly will extract meta tags from the response's <head>. To prevent meta tags from being updated, set [up-meta-tags=false].

This attribute is only used when updating history.

[up-lang] optional

An explicit language code to set as the html[lang] attribute.

By default Unpoly will extract the language from the response and update the html[lang] attribute in the current page. To prevent the attrribute from being changed, set [up-lang=false].

This attribute is only used when updating history.

[up-transition] optional

The name of an transition to morph between the old and few fragment.

If you are prepending or appending content, use the [up-animation] attribute instead.

[up-animation] optional

The name of an animation to reveal a new fragment when prepending or appending content.

If you are replacing content (the default), use the [up-transition] attribute instead.

[up-duration] optional

The duration of the transition or animation (in millisconds).

[up-easing] optional

The timing function that accelerates the transition or animation.

See MDN documentation for a list of available timing functions.

[up-cache='auto'] optional

Whether to read from and write to the cache.

With [up-cache=true] Unpoly will try to re-use a cached response before connecting to the network. To prevent display of stale content, cached responses are reloaded once rendered. If no cached response exists, Unpoly will make a request and cache the server response.

With [up-cache=auto] Unpoly will use the cache only if up.network.config.autoCache returns true for the request. By default this only caches GET requests.

With [up-cache=false] Unpoly will always make a network request.

[up-revalidate='auto'] optional

Whether to reload the targeted fragment after it was rendered from a cached response.

With [up-revalidate='auto'] Unpoly will revalidate if the up.fragment.config.autoRevalidate(response) returns true. By default this configuration will return true for expired responses.

With [up-revalidate='true'] Unpoly will always revalidate cached content, regardless of its age.

With [up-revalidate='false'] Unpoly will never revalidate cached content.

[up-expire-cache] optional

Whether existing cache entries will be expired with this request.

By default a non-GET request will expire the entire cache. You may also pass a URL pattern to only expire matching requests.

Also see up.request({ expireCache }) and up.network.config.expireCache.

[up-evict-cache] optional

Whether existing cache entries will be evicted with this request.

You may also pass a URL pattern to only evict matching requests.

Also see up.request({ evictCache }) and up.network.config.evictCache.

[up-abort='target'] optional

Whether to abort existing requests before rendering.

[up-abortable='true'] optional experimental

Whether this request may be aborted by other requests targeting the same fragments or layer.

See aborting requests for details.

[up-background='false'] optional

Whether this request will load in the background.

Background requests deprioritized over foreground requests. Background requests also won't emit up:network:late events and won't trigger the progress bar.

[up-bad-response-time] optional experimental

The number of milliseconds after which this request can cause an up:network:late event.

Defaults to up.network.config.badResponseTime.

[up-timeout] optional

The number of milliseconds after which this request fails with a timeout.

Defaults to up.network.config.timeout.

[up-layer='origin current'] optional

The layer in which to match and render the fragment.

See layer option for a list of allowed values.

To open the fragment in a new overlay, pass [up-layer=new]. In this case attributes for [up-layer=new] may also be used.

[up-peel='true'] optional

Whether to close overlays obstructing the updated layer when the fragment is updated.

This is only relevant when updating a layer that is not the frontmost layer.

[up-context] optional experimental

A JSON object that will be merged into the context of the current layer once the fragment is rendered.

[up-scroll='auto'] optional

How to scroll after the new fragment was rendered.

See Scrolling for a list of allowed values.

[up-scroll-behavior='instant'] optional
[up-reveal-snap] optional

When to snap to the top when scrolling to an element near the top edge of the viewport's scroll buffer.

[up-reveal-top] optional

When to move a revealed element to the top when scrolling to an element.

[up-reveal-padding] optional

How much space to leave to the closest viewport edge when scrolling to an element.

[up-reveal-max] optional

How many pixel lines of high element to reveal when scrolling to an element.

[up-save-scroll] optional

Whether to save scroll positions before updating the fragment.

Saved scroll positions can later be restored with [up-scroll=restore].

[up-focus='auto'] optional

What to focus after the new fragment was rendered.

See Controlling focus for a list of allowed values.

[up-save-focus] optional

Whether to save focus-related state before updating the fragment.

Saved scroll positions can later be restored with [up-focus=restore].

[up-confirm] optional

A message the user needs to confirm before fragments are updated.

The message will be shown as a native browser prompt.

If the user does not confirm the render promise will reject and no fragments will be updated.

[up-feedback='true'] optional

Whether to give the link an .up-active class and the targeted element an .up-loading class while loading content.

See navigation feedback.

[up-on-loaded] optional

A JavaScript snippet that is executed when the server responds with new HTML, but before the HTML is rendered.

The snippet runs in the following scope:

Expression Value
this The link being followed
event A preventable up:fragment:loaded event

The snippet will also run for failed responses.

[up-on-rendered] optional

A JavaScript snippet that is executed when Unpoly has updated fragments.

The snippet runs in the following scope:

Expression Value
this The link being followed
result The up.RenderResult for the respective render pass

The snippet will be called zero, one or two times:

  • When the server rendered an empty response, no fragments are updated. [up-on-rendered] is not called.
  • When the server rendered a matching fragment, it will be updated on the page. [up-on-rendered] is called with the result.
  • When revalidation renders a second time, [up-on-rendered] is called again with the final result.

Also see Running code after rendering.

[up-on-finished] optional

A JavaScript snippet that is execvuted when no further DOM changes will be caused by this render pass.

In particular:

Expression Value
this The link being followed
result The up.RenderResult for the last render pass that updated a fragment

If revalidation re-rendered the fragment, result describes updates from the second render pass. If no revalidation was performed, or if revalidation yielded an empty response, it is the result from the initial render pass.

Also see Awaiting postprocessing.

[up-on-offline] optional

A JavaScript snippet that is executed when the fragment could not be loaded due to a disconnect or timeout.

Expression Value
this The link being followed
error An up.Offline error
[up-on-error] optional

A JavaScript snippet that is run when any error is thrown during the rendering process.

Expression Value
this The link being followed
error An Error object

The callback is also called when the render pass fails due to network issues, or aborts.

Also see Handling errors.

[up-use-keep='true'] optional experimental

Whether [up-keep] elements will be preserved in the updated fragment.

[up-use-hungry='true'] optional experimental

Whether [up-hungry] elements outside the updated fragment will also be updated.