Edit this page

API Linking to fragments
module up.link

The up.link module lets you build links that update fragments instead of entire pages.

Motivation

In a traditional web application, the entire page is destroyed and re-created when the user follows a link:

Traditional page flow

This makes for an unfriendly experience:

  • State changes caused by AJAX updates get lost during the page transition.
  • Unsaved form changes get lost during the page transition.
  • The JavaScript VM is reset during the page transition.
  • If the page layout is composed from multiple scrollable containers (e.g. a pane view), the scroll positions get lost during the page transition.
  • The user sees a "flash" as the browser loads and renders the new page, even if large portions of the old and new page are the same (navigation, layout, etc.).

Unpoly fixes this by letting you annotate links with an [up-target] attribute. The value of this attribute is a CSS selector that indicates which page fragment to update. The server still renders full HTML pages, but we only use the targeted fragments and discard the rest:

Unpoly page flow

With this model, following links feels smooth. All DOM state outside the updated fragment is preserved. Pages also load much faster since the DOM, CSS and JavaScript environments do not need to be destroyed and recreated for every request.

Example

Let's say we are rendering three pages with a tabbed navigation to switch between screens:

Your HTML could look like this:

<nav>
  <a href="/pages/a">A</a>
  <a href="/pages/b">B</a>
  <a href="/pages/b">C</a>
</nav>

<article>
  Page A
</article>

Since we only want to update the <article> tag, we annotate the links with an up-target attribute:

<nav>
  <a href="/pages/a" up-target="article">A</a>
  <a href="/pages/b" up-target="article">B</a>
  <a href="/pages/b" up-target="article">C</a>
</nav>

Note

Instead of article you can use any other CSS selector like #main .article.

With these [up-target] annotations Unpoly only updates the targeted part of the screen. The JavaScript environment will persist and the user will not see a white flash while the new page is loading.


Guides

Features

Essentials

All features

HTML
a[up-dash] deprecated

Follows this link as fast as possible.

HTML
a[up-follow] stable

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

HTML
a[up-instant] stable

Follows this link on mousedown instead of click.

HTML
a[up-preload] stable

Preloads this link when the user hovers over it.

JS
up:click stable

A click event that honors the [up-instant] attribute.

HTML
[up-clickable] experimental

Enables keyboard interaction for elements that represent links or buttons.

HTML
[up-expand] stable

Add an [up-expand] attribute to any element to enlarge the click area of a descendant link.

JS
up.follow(link, [options]) stable

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

HTML
[up-href] stable

Makes any element behave like a hyperlink.

JS
up.link.config stable

Configures defaults for link handling.

JS
up:link:follow stable

This event is emitted when a link is followed through Unpoly.

JS
up.link.followOptions(link, [options]) stable

Parses the render options that would be used to follow the given link, but does not render.

JS
up.link.isFollowable(link) stable

Returns whether the given link will be followed by Unpoly instead of making a full page load.

JS
up.link.isSafe(link) stable

Returns whether the given link has a safe HTTP method like GET.

JS
up.link.makeFollowable(link) experimental

Makes sure that the given link will be followed by Unpoly instead of making a full page load.

JS
up.link.preload(link, options, [options]) stable

Preloads the given link.

JS
up:link:preload stable

This event is emitted before a link is preloaded.