Browse AjaxPress docs

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:

WhereRunsUse it for
The page, inside the iframeOn every page, as a normal page loadAlmost everything: sliders, forms, widgets, analytics
The top windowOnce per visit: the first page’s scripts, which stay loadedCode 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 propertyTypeValue
urlstringThe page’s address
titlestringThe page’s title

ajaxpress-loading

A navigation, a form submission or a prefetch has started.

detail propertyTypeValue
urlstringThe address being loaded
prefetchbooleantrue 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 propertyTypeValue
urlstringThe new address
fromPrefetchbooleantrue 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

AttributeOnEffect
data-ajaxpress-persistAny elementKeeps it alive across pages (a persistent player)
class="ajaxpress-persist"Any elementSame
id="persistent-…"Any elementSame, for any id starting with persistent-
data-persist<audio>, <video>Same
data-persist-key="…"A persistent elementIts identity across pages. Recommended
data-ajaxpress-ignore-submitA form, or any element around formsThe form submits the normal way
downloadA linkAlways left to the browser
target="_blank", _top, _parentA link or formAlways 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:

  1. data-persist-key, as written.
  2. The element’s id.
  3. The src of the first iframe, audio or video inside it.
  4. The element’s own src, for an audio or video element.
  5. 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-persist in the top window, inside a wrapper div with data-ajaxpress-lifted and data-persist-key set 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:

ElementWhat it is
iframe#ajaxpress-containerThe page on screen
iframe#ajaxpress-backgroundThe hidden frame where the next page loads
div#ajaxpress-persistHolds persistent elements. position: fixed, z-index: 1000000
#ajaxpress-loaderThe progress bar or spinner
style#ajaxpress-custom-cssYour Custom CSS setting
html.ajaxpress-cursor-activePresent 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.