up.fragment
JavaScript namespace
The JavaScript API to render, look up and destroy page fragments. Every link, form, overlay or poll that Unpoly handles ends in up.render(), and your own code can call it with the same options.
up.render() replaces elements on the current page with matching elements from a server response or an HTML string. up.navigate() does the same with the defaults of a user navigation, like updating history and scrolling to the new content. up.reload() fetches a fresh copy of a fragment from the URL it was loaded from, and up.destroy() removes a fragment, with an optional animation.
Lookup functions like up.fragment.get() differ from document.querySelector(): they only see the current layer, ignore elements that are playing out their exit animation, and understand Unpoly's own selectors like :main or :origin. Elements marked [up-keep] are kept across updates. For low-level DOM utilities that complement the browser's native API, see up.element.
Features
-
:layer TARGET
Your target selectors may use this pseudo-selector to replace the layer's topmost swappable element.
-
:main TARGET
A pseudo-selector that matches the layer's main fragment.
-
:maybe TARGET
Marks a target selector as optional.
-
:none TARGET
To make a server request without changing a fragment, use the
:nonetarget. -
:origin TARGET
Your target selectors may use this pseudo-selector to reference the origin element that triggered the change.
-
Returns the current context.
-
Destroys the given element or selector.
-
.up-destroying CSS
Elements are assigned the
.up-destroyingclass before they are destroyed or while they are being removed by a transition. -
[up-etag] ATTR
Sets an ETag for the fragment's underlying data.
-
Updates a selector on the current page with the same selector from the given HTML string.
-
Aborts requests targeting a fragment or layer.
-
up:fragment:aborted EVENT
This event is emitted when requests for an element were aborted.
-
Returns all elements matching the given CSS selector, but ignores elements that are being destroyed or that are being removed by a transition.
-
Returns the first element that matches the selector by testing the element itself and traversing up through ancestors in element's layers.
-
up.fragment.config CONFIG
Configures defaults for fragment updates.
-
Returns whether the given
rootmatches or contains the given selector or element. -
up:fragment:destroyed EVENT
This event is emitted after a page fragment was destroyed and removed from the DOM.
-
Returns the ETag of the content in the given element.
-
Returns the first element matching the given selector, but ignores elements that are being destroyed or that are being removed by a transition.
-
Returns the first fragment matching the given CSS selector.
-
up:fragment:inserted EVENT
When a page fragment has been inserted or updated, this event is emitted on the new element.
-
Returns whether the given fragment is both attached to the DOM and not currently in a destroy animation.
-
Returns whether Unpoly can derive a target selector for the given element.
-
up:fragment:keep EVENT
This event is emitted before an existing element is kept during a render pass.
-
The
up:fragment:keptevent is emitted after an existing element has been kept during a render pass. -
up:fragment:loaded EVENT
This event is emitted after the server response was loaded, but before the HTML is used to change a fragment.
-
Returns whether the given element matches the given CSS selector or other element.
-
up:fragment:offline EVENT
This event is emitted when the device loses its network connection while rendering content.
-
Runs a callback when the given element or its ancestors in the same layer were aborted.
-
Runs a callback when the given element or its ancestors in the same layer were kept.
-
Returns the URL from which the given element was loaded.
-
Returns a list of a
rootelement's descendants matching the given selector. -
Returns the last modification time of the content in the given element.
-
Derives a CSS selector that matches the given element as good as possible.
-
[up-id] ATTR
Sets a unique identifier for this element.
-
[up-keep] ATTR
Elements with an
[up-keep]attribute will be kept when rendering. -
[up-main] ATTR
Marks this element as the main fragment of your application layout.
-
Navigates to the given URL by updating a major fragment in the current page.
-
Replaces the given element with a fresh copy fetched from the server.
-
Replaces elements on the current page with matching elements from a server response or HTML string.
-
Replaces elements on the current page with corresponding elements from a new page fetched from the server.
-
[up-source] ATTR
When Unpoly inserts a fragment, the
[up-source]attribute is automatically set to the URL from which the fragment's HTML was loaded. -
Clones a template element.
-
up:template:clone EVENT
This event is emitted before a template is cloned.
-
[up-time] ATTR
Sets the time when the fragment's underlying data was last changed.
-
Fetches the given URL with JavaScript and replaces the current layer's main fragment with a matching fragment from the server response.