Edit this page

API Fragment API
module up.fragment

The up.fragment module offers a high-level JavaScript API to work with DOM elements.

A fragment is an element with some additional properties that are useful in the context of a server-rendered web application:

Differences to up.element

Functions in up.fragment by default only see elements on the current layer. They also support non-standard CSS extensions like :main or :has().

For low-level DOM utilities that complement the browser's native API, see up.element.


Guides

Features

Essentials

All features

CSS
:has() stable

Your target selectors may use this pseudo-class to replace an element with an descendant matching the given selector.

CSS
:layer experimental

Your target selectors may use this pseudo-selector to replace the layer's topmost swappable element.

CSS
:main stable

A pseudo-selector that matches the layer's main content area.

CSS
:maybe stable

Marks a target selector as optional.

CSS
:none stable

To make a server request without changing a fragment, use the :none target.

CSS
:origin stable

Your target selectors may use this pseudo-selector to reference the origin element that triggered the change.

JS
up.context experimental

Returns the current context.

JS
up.destroy(target, [options]) stable

Destroys the given element or selector.

HTML
.up-destroying stable

Elements are assigned the .up-destroying class before they are destroyed or while they are being removed by a transition.

HTML
[up-etag] experimental

Sets an ETag for the fragment's underlying data.

JS
up.extract(target, html, [options]) deprecated

Updates a selector on the current page with the same selector from the given HTML string.

JS
up.fragment.abort([element], [options]) experimental

Aborts requests targeting a fragment or layer.

JS
up:fragment:aborted experimental

This event is emitted when requests for an element were aborted.

JS
up.fragment.all([root], selector, [options]) stable

Returns all elements matching the given CSS selector, but ignores elements that are being destroyed or that are being removed by a transition.

JS
up.fragment.closest(element, selector) stable

Returns the first element that matches the selector by testing the element itself and traversing up through ancestors in element's layers.

JS
up.fragment.config stable

Configures defaults for fragment updates.

JS
up.fragment.contains(root, query) experimental

Returns whether the given root matches or contains the given selector or element.

JS
up:fragment:destroyed stable

This event is emitted after a page fragment was destroyed and removed from the DOM.

JS
up.fragment.etag(element) experimental

Returns the ETag of the content in the given element.

JS
up.fragment.first([root], selector, [options]) deprecated

Returns the first element matching the given selector, but ignores elements that are being destroyed or that are being removed by a transition.

JS
up.fragment.get([root], selector, [options]) stable

Returns the first fragment matching the given CSS selector.

JS
up:fragment:inserted stable

When any page fragment has been inserted or updated, this event is emitted on the fragment.

JS
up.fragment.isTargetable(element) experimental

Returns whether Unpoly can derive a target selector for the given element.

JS
up:fragment:keep stable

This event is emitted before an existing element is kept during a page update.

JS
up:fragment:loaded stable

This event is emitted after the server response was loaded, but before the HTML is used to change a fragment.

JS
up.fragment.matches(fragment, selector, [options]) experimental

Returns whether the given element matches the given CSS selector or other element.

JS
up:fragment:offline experimental

This event is emitted when the device loses its network connection while rendering content.

JS
up.fragment.onAborted(element, callback) experimental

Runs a callback when the given element or its ancestors were aborted.

JS
up.fragment.source(element) stable

Returns the URL the given element was retrieved from.

JS
up.fragment.subtree(parent, selector, [options]) experimental

Returns a list of the given parent's descendants matching the given selector. The list will also include the parent element if it matches the selector itself.

JS
up.fragment.time(element) experimental

Returns the last modification time of the content in the given element.

JS
up.fragment.toTarget(element, [options]) stable

Derives a CSS selector that matches the given element as good as possible.

HTML
[up-id] stable

Sets an unique identifier for this element.

HTML
[up-keep] stable

Elements with an [up-keep] attribute will be persisted during fragment updates.

HTML
[up-main] stable

Marks this element as the primary content element of your application layout.

JS
up.navigate([target], [options]) stable

Navigates to the given URL by updating a major fragment in the current page.

JS
up.reload([target], [options]) stable

Replaces the given element with a fresh copy fetched from the server.

JS
up.render([target], [options]) stable

Replaces elements on the current page with matching elements from a server response or HTML string.

JS
up.replace(target, url, [options]) deprecated

Replaces elements on the current page with corresponding elements from a new page fetched from the server.

HTML
[up-source] stable

When Unpoly inserts a fragment, the [up-source] attribute is automatically set to the URL from which the fragment's HTML was loaded.

HTML
[up-time] experimental

Sets the time when the fragment's underlying data was last changed.

JS
up.visit(url, [options]) stable

Fetches this given URL with JavaScript and replaces the current layer's main element with a matching fragment from the server response.