JavaScript events and markup reference
On this page
A reference for developers: the events AjaxPress fires, the HTML attributes it understands, and the elements it adds. It is accurate for AjaxPress 2.4.3 and later.
Where your code runs
AjaxPress keeps the first page a visitor loads as a top window (the shell), and shows every page, the first one included, inside a full-screen iframe on top of it. That gives two places where code can run:
| Where | Runs | Use it for |
|---|---|---|
| The page, inside the iframe | On every page, as a normal page load | Almost everything: sliders, forms, widgets, analytics |
| The top window | Once per visit: the first page’s scripts, which stay loaded | Code that must follow the visitor across pages |
Code on your pages therefore needs no changes. DOMContentLoaded, load and jQuery’s ready fire on every page, exactly as without AjaxPress.
AjaxPress’s events are dispatched in the top window only. To listen for them from a site-wide script, which runs in both places, register the listener in the top window alone:
if (window.self === window.top) {
document.addEventListener('ajaxpress:ready', (e) => {
console.log('Now showing', e.detail.url);
});
}
Don’t add listeners to window.top.document from code inside the page. Every page would add another one, and they pile up.
To tell whether code is running inside AjaxPress’s page frame:
const inAjaxPressFrame = window.self !== window.top &&
!!window.top.document.getElementById('ajaxpress-persist');
Events
All events are dispatched on the top window’s document, and they bubble.
ajaxpress:ready
The page on screen is ready, and its address, title and head tags (canonical, Open Graph, JSON-LD) are in place. It fires once for the first page and once after every navigation, including Back and Forward.
detail property | Type | Value |
|---|---|---|
url | string | The page’s address |
title | string | The page’s title |
ajaxpress-loading
A navigation, a form submission or a prefetch has started.
detail property | Type | Value |
|---|---|---|
url | string | The address being loaded |
prefetch | boolean | true for a background prefetch, which may never be shown |
ajaxpress-complete
A navigation has finished, and the new page is on screen. It doesn’t fire for the first page.
detail property | Type | Value |
|---|---|---|
url | string | The new address |
fromPrefetch | boolean | true when the page was already prefetched |
ajaxpress_page_view (dataLayer)
After each navigation (not the first page), AjaxPress pushes this to the top window’s window.dataLayer:
{
event: 'ajaxpress_page_view',
page_location: 'https://example.com/new-page/',
page_title: 'New page',
engagement_time_msec: 12840 // foreground time on the previous page
}
See Analytics, Tag Manager and consent banners before using it for page views.
Attributes AjaxPress reads
| Attribute | On | Effect |
|---|---|---|
data-ajaxpress-persist | Any element | Keeps it alive across pages (a persistent player) |
class="ajaxpress-persist" | Any element | Same |
id="persistent-…" | Any element | Same, for any id starting with persistent- |
data-persist | <audio>, <video> | Same |
data-persist-key="…" | A persistent element | Its identity across pages. Recommended |
data-ajaxpress-ignore-submit | A form, or any element around forms | The form submits the normal way |
download | A link | Always left to the browser |
target="_blank", _top, _parent | A link or form | Always left to the browser |
How a persistent element is identified
AjaxPress needs to recognise a persistent element as the same one on every page. It uses the first of these that applies:
data-persist-key, as written.- The element’s
id. - The
srcof the firstiframe,audioorvideoinside it. - The element’s own
src, for anaudioorvideoelement. - The first 200 characters of its HTML.
Set data-persist-key whenever the id or media URL could differ between pages.
What happens to a persistent element
- First page that has it: the live element is moved, not copied, into
#ajaxpress-persistin the top window, inside a wrapperdivwithdata-ajaxpress-liftedanddata-persist-keyset to its identity. Its listeners, playback and inner iframes survive. - Later pages that have it: their copy is removed, and the one already running stays.
- A page without it: the running element is stopped and removed.
Styles scoped to its old parent no longer apply once it has moved. Target it inside the container instead:
#ajaxpress-persist [data-persist-key="live-radio"] {
position: fixed;
left: 0;
right: 0;
bottom: 0;
}
Elements AjaxPress adds
In the top window:
| Element | What it is |
|---|---|
iframe#ajaxpress-container | The page on screen |
iframe#ajaxpress-background | The hidden frame where the next page loads |
div#ajaxpress-persist | Holds persistent elements. position: fixed, z-index: 1000000 |
#ajaxpress-loader | The progress bar or spinner |
style#ajaxpress-custom-css | Your Custom CSS setting |
html.ajaxpress-cursor-active | Present while a page loads |
In each page, AjaxPress adds <meta name="robots" content="noindex,nofollow" data-ajaxpress-iframe-only>. It tells crawlers not to index the framed copy of a page separately from the page itself, and it never reaches the top window’s <head>. Crawlers don’t get AjaxPress at all, so they only ever see your normal pages.
Extending AjaxPress
To add persistent selectors, bypass rules, a custom loader or a custom transition, see the JavaScript extension API. For server-side hooks, see PHP hooks and filters.