Lightweight AJAX navigation library — accelerate your website without a JS framework.
µJS intercepts clicks on links and form submissions to load pages via AJAX instead of full browser navigation. The fetched content replaces part (or all) of the current page, making navigation faster and smoother.
Built on the modern Fetch API (no XMLHttpRequest), with native AbortController for clean request cancellation.
No build step required. No dependencies. No framework. Just a single <script> tag.
Inspired by pjax, Turbo and HTMX, µJS aims to be simpler and lighter while covering the most common use cases.
- 🚀 Fast — Prefetch on hover, no full page reload, progress bar
- 🪶 Lightweight — Single file, ~7 KB gzipped, zero dependencies
- 🔌 Drop-in — Works with any backend (PHP, Python, Ruby, Go…), no server-side changes needed
- 🧩 Patch mode — Update multiple page fragments in a single request
- 🎯 Triggers — Any element, any event: live search, polling, focus actions
- 🔄 HTTP verbs — GET, POST, PUT, PATCH, DELETE on links, buttons, and forms
- 📡 SSE — Real-time updates via Server-Sent Events
- 🌐 Modern Fetch API — Native
fetch()withAbortController(no XMLHttpRequest) - ✨ Modern UX — View Transitions, DOM morphing (via idiomorph), event delegation
- Installation
- Quick start
- Modes
- Patch mode
- Forms
- HTTP methods
- HTTP headers
- Triggers
- Server-Sent Events (SSE)
- History & Scroll
- Scroll restoration
- Prefetch
- DOM morphing
- View Transitions
- Scripts
- Progress bar
- Events
- Error handling
- Attributes reference
- Configuration reference
- Programmatic API
- Browser support
- License
Place the scripts in the <head>, so that they are executed only once. Scripts of the <body> are executed again on each navigation (see Scripts). µJS waits for the DOM to be ready when needed.
<head>
<!-- your metadata and stylesheets -->
<script src="/path/to/mu.min.js"></script>
<script>mu.init();</script>
</head><!-- unpkg -->
<script src="https://unpkg.com/@digicreon/mujs/dist/mu.min.js"></script>
<!-- jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/@digicreon/mujs/dist/mu.min.js"></script>
<script>mu.init();</script>With Subresource Integrity (SRI):
<!-- unpkg -->
<script src="https://unpkg.com/@digicreon/mujs@1.4.8/dist/mu.min.js"
integrity="sha384-x9kao8F430kRoLUGlLYGRC2FPo+rCsVlMUItQA1zbf0ZsIcht6wv8nikdx/Prqh2"
crossorigin="anonymous"></script>
<!-- jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/@digicreon/mujs@1.4.8/dist/mu.min.js"
integrity="sha384-x9kao8F430kRoLUGlLYGRC2FPo+rCsVlMUItQA1zbf0ZsIcht6wv8nikdx/Prqh2"
crossorigin="anonymous"></script>
<script>mu.init();</script>npm install @digicreon/mujsThen import it in your JavaScript:
import mu from "@digicreon/mujs";
mu.init();The package contains a classic script (dist/mu.min.js, which defines the global variable mu) and an ES module (dist/mu.mjs).
After calling mu.init(), all same-origin links are automatically intercepted. Clicking a link fetches the page via AJAX and replaces the current <body> with the fetched <body>. The page title is updated automatically. Browser history (back/forward buttons) works as expected.
µJS handles absolute paths (/about), relative URLs (about, ../about), and absolute same-origin URLs (https://yoursite.com/about). If your site uses a <base> tag, relative URLs are resolved accordingly. Note that only the <base> tag from the initially loaded page is used; <base> tags in dynamically fetched pages are not applied.
<!DOCTYPE html>
<html>
<head>
<title>My site</title>
<script src="/path/to/mu.min.js"></script>
<script>mu.init();</script>
</head>
<body>
<!-- These links are automatically handled by µJS -->
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>
<main id="content">
<p>Page content here.</p>
</main>
<!-- This link is NOT handled (external URL) -->
<a href="https://example.com">External link</a>
<!-- This link is NOT handled (has target attribute) -->
<a href="/page" target="_blank">Opens in new tab</a>
<!-- This link is NOT handled (has download attribute) -->
<a href="/file.pdf" download>Download PDF</a>
<!-- This link is NOT handled (explicitly disabled) -->
<a href="/page" mu-disabled>Disabled link</a>
</body>
</html>Clicks and submissions already handled by another script (which called preventDefault()) are ignored. Links to documents that are not HTML pages (PDF, images, downloads...) are loaded by the browser.
To replace only a fragment of the page instead of the whole body:
<a href="/about" mu-target="#content" mu-source="#content">About</a>This fetches /about, extracts the #content element from the response, and replaces the current #content with it.
The mu-mode attribute controls how fetched content is injected into the page. Default: replace.
| Mode | Description |
|---|---|
replace |
Replace the target node with the source node (default). |
update |
Replace the inner content of the target with the source's inner content. |
prepend |
Insert the source node at the beginning of the target. |
append |
Insert the source node at the end of the target. |
before |
Insert the source node before the target. |
after |
Insert the source node after the target. |
remove |
Remove the target node (source is ignored). |
none |
Do nothing to the DOM (events are still fired). |
patch |
Process multiple targeted fragments (see Patch mode). |
If the source is the <body> (default), its content is inserted, not the <body> element itself.
Example:
<a href="/notifications" mu-mode="update" mu-target="#notifs" mu-source="#notifs">
Refresh notifications
</a>Patch mode allows a single request to update multiple parts of the page. The server returns HTML fragments, each annotated with a target and an optional mode.
<a href="/api/comments/new" mu-mode="patch">Add comment</a>The server returns plain HTML. Each element with a mu-patch-target attribute is a patch fragment:
<!-- Replaces #comment-42 (default mode: replace) -->
<div class="comment" mu-patch-target="#comment-42">
Updated comment text
</div>
<!-- Appends a new comment to #comments -->
<div class="comment" mu-patch-target="#comments" mu-patch-mode="append">
New comment
</div>
<!-- Updates the page title -->
<title mu-patch-target="title">New page title</title>
<!-- Adds a stylesheet -->
<link rel="stylesheet" href="/css/gallery.css"
mu-patch-target="head" mu-patch-mode="append">
<!-- Removes an element -->
<div mu-patch-target="#old-banner" mu-patch-mode="remove"></div>Patch fragments are standard HTML elements — no special tags needed. The mu-patch-* attributes are preserved on injected nodes for debugging.
The mu-patch-mode attribute accepts the same values as mu-mode (except patch and none). Default is replace.
By default, patch mode does not modify browser history. To add the URL to history:
<a href="/products?cat=3" mu-mode="patch" mu-patch-history="true">Filter</a>µJS intercepts form submissions. HTML5 validation (reportValidity()) is checked before any request. When a form has multiple submit buttons with different name/value attributes, µJS includes the clicked button's data in the submission. The formaction, formmethod, formenctype and formnovalidate attributes of the button are respected; a button with formtarget is left to the browser.
Data is serialized as a query string. Behaves like a link. As browsers do, µJS replaces the query string of the action attribute; the one of mu-url is kept.
<form action="/search" method="get" mu-target="#results" mu-source="#results">
<input type="text" name="q">
<button type="submit">Search</button>
</form>Data is sent as application/x-www-form-urlencoded by default. Add enctype="multipart/form-data" for file uploads. History is disabled by default (POST responses should not be replayed via the browser back button).
<form action="/comment/create" method="post">
<textarea name="body"></textarea>
<button type="submit">Send</button>
</form>Use mu-method to override the HTTP method. The form data is sent like POST (application/x-www-form-urlencoded by default).
<!-- PUT form -->
<form action="/api/user/1" mu-method="put">
<input type="text" name="name">
<button type="submit">Update</button>
</form>
<!-- DELETE form (no data needed) -->
<form action="/api/user/1" mu-method="delete">
<button type="submit">Delete</button>
</form><form action="/comment/create" method="post" mu-mode="patch">
<textarea name="body"></textarea>
<button type="submit">Send</button>
</form>Server response:
<div class="comment" mu-patch-target="#comments" mu-patch-mode="append">
<p>The new comment</p>
</div>
<form action="/comment/create" method="post" mu-patch-target="#comment-form">
<textarea name="body"></textarea>
<button type="submit">Send</button>
</form>The new comment is appended to the list, and the form is replaced with a blank version.
When the server answers to a non-GET form with an error status (like 422 Unprocessable Content) and an HTML content, this content is displayed as usual, according to the mode, target and source of the form. If the page doesn't contain the expected source, the whole body is replaced. See Error handling.
<form action="/save" method="post" mu-validate="myValidator">...</form>
<script>
function myValidator(form) {
return form.querySelector('#name').value.length > 0;
}
</script>Add mu-confirm-quit to a form. If any input is modified, the user is prompted before navigating away:
<form action="/save" method="post" mu-confirm-quit>
<input type="text" name="title">
<button type="submit">Save</button>
</form>By default, links use GET and forms use their method attribute. The mu-method attribute overrides the HTTP method for any element.
Supported values: get, post, put, patch, delete, sse.
<!-- DELETE button -->
<button mu-url="/api/item/42" mu-method="delete" mu-mode="remove" mu-target="#item-42">
Delete
</button>
<!-- PUT link -->
<a href="/api/publish/5" mu-method="put" mu-mode="none">Publish</a>µJS sends the following HTTP headers with each request:
| Header | Value | Sent when |
|---|---|---|
Accept |
text/html, application/xhtml+xml, */*;q=0.1 |
Always |
X-Requested-With |
XMLHttpRequest |
Always |
X-Mu-Mode |
Current injection mode | Always |
X-Mu-Method |
HTTP method (POST, PUT, PATCH, DELETE) |
Non-GET requests only |
X-Mu-Prefetch |
1 |
Prefetch requests only |
These headers allow the server to detect µJS requests and adapt its response (e.g. return a fragment instead of a full page, skip analytics, return lighter content on prefetch).
µJS supports custom event triggers via the mu-trigger attribute. This allows any element with a mu-url to initiate a fetch on events other than click or submit.
When mu-trigger is absent, the trigger depends on the element type:
| Element | Default trigger |
|---|---|
<a> |
click |
<form> |
submit |
<input>, <textarea>, <select> |
change |
| Any other element | click |
| Trigger | Browser event(s) | Typical elements |
|---|---|---|
click |
click |
Any element (default for <a>, <button>, <div>...) |
submit |
submit |
<form> |
change |
input |
<input>, <textarea>, <select> |
blur |
change + blur (deduplicated) |
<input>, <textarea>, <select> |
focus |
focus |
<input>, <textarea>, <select> |
load |
(fires immediately when rendered) | Any element |
Live search with debounce:
<input type="text" name="q"
mu-trigger="change" mu-debounce="500"
mu-url="/search" mu-target="#results" mu-source="#results"
mu-mode="update">Action on focus (e.g. load suggestions):
<input type="text" mu-trigger="focus"
mu-url="/suggestions" mu-target="#suggestions" mu-mode="update">Action on blur (save on field exit):
<input type="text" name="title" mu-trigger="blur"
mu-url="/api/save" mu-method="put" mu-target="#status" mu-mode="update">Load content immediately:
<div mu-trigger="load"
mu-url="/sidebar" mu-target="#sidebar" mu-mode="update">
</div>Combine mu-trigger="load" with mu-repeat to poll a URL at regular intervals:
<div mu-trigger="load" mu-repeat="5000"
mu-url="/notifications" mu-target="#notifs" mu-mode="update">
</div>The first fetch fires immediately, then every 5 seconds. Polling intervals are automatically cleaned up when the element is removed from the DOM.
Use mu-debounce to delay the fetch until the user stops interacting:
<input type="text" name="q" mu-debounce="300"
mu-url="/search" mu-target="#results" mu-mode="update">The value of a field (<input>, <textarea>, <select>) that has a name is sent with the request. If the field is in a form, all the data of the form are sent. If a field triggers several requests, a late response never overwrites a more recent one.
Note: Triggers other than
clickandsubmitdefault to no browser history entry and no scroll (mu-history="false",mu-scroll="false").
µJS supports real-time updates via Server-Sent Events. Set mu-method="sse" to open an EventSource connection instead of a one-shot fetch.
<div mu-trigger="load" mu-url="/chat/stream"
mu-mode="patch" mu-method="sse">
</div>Each incoming SSE message is treated as HTML and rendered according to the element's mu-mode. In patch mode, the server sends HTML fragments with mu-patch-target attributes, just like a regular patch response.
event: message
data: <div mu-patch-target="#messages" mu-patch-mode="append"><p>New message!</p></div>
event: message
data: <span mu-patch-target="#online-count">42</span>
- No custom headers:
EventSourcedoes not support custom HTTP headers. Use query parameters for authentication (e.g.mu-url="/stream?token=abc"). - Connection limit: Browsers allow ~6 SSE connections per domain in HTTP/1.1. Use HTTP/2 to avoid this limit.
- Automatic cleanup: SSE connections are closed when the element is removed from the DOM (e.g. when the page changes).
mu-history controls whether the URL is added to browser history. mu-scroll controls whether the page scrolls to top after rendering. Both attributes are independent.
<!-- Skip history on a link -->
<a href="/panel" mu-history="false">Open panel</a>
<!-- Skip history globally -->
<script>mu.init({ history: false });</script>
<!-- Scroll to top without adding history -->
<a href="/page" mu-history="false" mu-scroll="true">Link</a>Defaults for mu-history and mu-scroll depend on the mode and context:
| Mode | Context | mu-history |
mu-scroll |
|---|---|---|---|
replace, update |
Links (GET) | true |
true |
replace, update |
Forms (GET) | true |
true |
replace, update |
Forms (POST/PUT/PATCH/DELETE) | false |
true |
replace, update |
Triggers (change, blur, focus, load) | false |
false |
replace, update |
SSE | false |
false |
append, prepend, before, after, remove, none |
Any | false |
false |
patch |
Any | false |
false |
Redirections always add the URL to browser history, regardless of the mu-history setting. In patch mode, use mu-patch-history="true" to add the URL to history.
When the user navigates with the browser's back/forward buttons, µJS automatically restores the scroll position to where it was before leaving the page. This works out of the box — no configuration needed.
When enabled (default), µJS fetches the target page when the user hovers over a link, before they click. A 50ms delay filters accidental hover-throughs (mouse passing over a link without intent to click). This saves ~100-300ms of perceived loading time.
The prefetch cache stores one entry per URL and mode. Responses are reused for a short time (prefetchTtl option, 3 seconds by default), and the cache is emptied by any non-GET request. Prefetch only applies to GET requests — elements with mu-method="post", put, patch, delete or sse are never prefetched.
<!-- Disable prefetch on a specific link -->
<a href="/heavy-page" mu-prefetch="false">Heavy page</a>
<!-- Disable prefetch globally -->
<script>mu.init({ prefetch: false });</script>When a morph library is available, µJS uses it for replace and update modes to preserve DOM state (input focus, scroll positions, video playback, CSS transitions, etc.).
µJS auto-detects idiomorph. Just load it before µJS:
<script src="/path/to/idiomorph.js"></script>
<script src="/path/to/mu.min.js"></script>
<script>mu.init();</script>If idiomorph is not loaded, µJS falls back to direct DOM replacement. No error, no warning.
Disable morphing globally or per-element:
<!-- Globally -->
<script>mu.init({ morph: false });</script>
<!-- Per-element -->
<a href="/page" mu-morph="false">Link</a>To use a different morph library:
mu.init();
mu.setMorph(function(target, html, opts) {
myMorphLib.morph(target, html, opts);
});µJS uses the View Transitions API when supported by the browser, providing smooth animated transitions between page states.
Enabled by default. Falls back silently on unsupported browsers.
<!-- Disable globally -->
<script>mu.init({ transition: false });</script>
<!-- Disable per-element -->
<a href="/page" mu-transition="false">Link</a>When µJS loads a page, scripts are handled differently depending on their location:
<head>scripts are merged additively. If a script (identified by itssrcor content) is already present in the current<head>, it is not re-added or re-executed. New scripts found in the fetched page's<head>are added and executed once.<body>scripts found in the injected content are re-executed on each navigation. External scripts (withsrc) are loaded only once — subsequent navigations skip them. Inline scripts are re-executed every time.
This means third-party scripts loaded in the <head> (analytics, tracking, widgets) are not re-executed by µJS. For example, a Plausible or Google Analytics script in the <head> will detect SPA navigations on its own via pushState interception — no special configuration is needed.
To prevent a <body> inline script from being re-executed on navigation, add mu-disabled:
<script mu-disabled>
// This script runs only on the initial page load
oneTimeSetup();
</script>For code that should run after every µJS navigation, use the mu:after-render event instead of inline scripts:
document.addEventListener("mu:after-render", function(e) {
myApp.initWidgets();
});A thin progress bar (3px, blue) is displayed at the top of the page during fetch requests. It requires no external stylesheet.
The bar element has id="mu-progress" and can be customized via CSS:
#mu-progress {
background: red !important;
height: 5px !important;
}Disable globally:
<script>mu.init({ progress: false });</script>µJS dispatches CustomEvent events on document. All events carry a detail object with lastUrl and previousUrl.
| Event | Cancelable | Description |
|---|---|---|
mu:init |
No | Fired after initialization. |
mu:before-fetch |
Yes | Fired before fetching. preventDefault() aborts the load. |
mu:before-render |
Yes | Fired after fetch, before DOM injection. detail.html can be modified. |
mu:after-render |
No | Fired after DOM injection. |
mu:fetch-error |
Yes | Fired on fetch failure or HTTP error. preventDefault() disables the error handling. |
document.addEventListener("mu:after-render", function(e) {
console.log("Loaded: " + e.detail.url);
myApp.initWidgets();
});document.addEventListener("mu:before-fetch", function(e) {
if (e.detail.url === "/restricted") {
e.preventDefault();
}
});document.addEventListener("mu:before-render", function(e) {
e.detail.html = e.detail.html.replace("foo", "bar");
});document.addEventListener("mu:fetch-error", function(e) {
if (e.detail.status === 404) {
e.preventDefault();
alert("Page not found");
}
});When a request fails (HTTP error or network failure), the mu:fetch-error event is fired. Its detail object contains url, fetchUrl, config, sourceElement, and either status and response, or error. Then:
| Request | Behavior |
|---|---|
Page navigation: GET link or form, or mu.load(), in replace or update mode, with browser history |
The URL is loaded natively, so that the browser displays the error page. This also covers redirections to another site. |
| Back/forward buttons | The page is reloaded natively. |
Non-GET form in replace, update or patch mode, with an HTML response |
The response is displayed (see Validation errors). |
| Other cases: triggers, SSE, other modes, non-HTML responses, network failure on a non-GET request... | Nothing more. A non-GET request is never sent again. |
Nothing is loaded natively if the browser is offline.
Call preventDefault() on the event to disable this behavior for a request, or disable it globally:
<script>mu.init({ errorFallback: false });</script>All attributes support both mu-* and data-mu-* syntax.
| Attribute | Description |
|---|---|
mu-disabled |
Disable µJS on this element. |
mu-mode |
Injection mode (replace, update, prepend, append, before, after, remove, none, patch). |
mu-target |
CSS selector for the target node in the current page. |
mu-source |
CSS selector for the source node in the fetched page. |
mu-url |
Override the URL to fetch (instead of href / action). |
mu-prefix |
URL prefix for the fetch request. |
mu-title |
Selector for the title node. Supports selector/attribute syntax. Empty string to disable. |
mu-history |
Add URL to browser history (true/false). Default depends on mode and context. |
mu-scroll |
Force (true) or prevent (false) scrolling to top. Default depends on mode and context. |
mu-morph |
Disable morphing on this element (false). |
mu-transition |
Disable view transitions on this element (false). |
mu-prefetch |
Disable prefetch on hover for this link (false). |
mu-method |
HTTP method: get, post, put, patch, delete, or sse. |
mu-trigger |
Event trigger: click, submit, change, blur, focus, load. |
mu-debounce |
Debounce delay in milliseconds (e.g. "500"). |
mu-repeat |
Polling interval in milliseconds (e.g. "5000"). |
mu-confirm |
Show a confirmation dialog before loading (links, forms and triggers). |
mu-confirm-quit |
(Forms) Prompt before leaving if the form has been modified. |
mu-validate |
(Forms) Name of a JS validation function. Must return true/false. |
mu-patch-target |
(Patch fragments) CSS selector of the target node. |
mu-patch-mode |
(Patch fragments) Injection mode for this fragment. |
mu-patch-history |
Set to true to add the URL to browser history in patch mode. Default: false. |
Pass an object to mu.init() to override defaults:
mu.init({
history: false,
processForms: false,
morph: false,
progress: true
});| Option | Type | Default | Description |
|---|---|---|---|
processLinks |
bool | true |
Intercept <a> tags. |
processForms |
bool | true |
Intercept <form> tags. |
history |
bool | true |
Add URL to browser history. |
mode |
string | "replace" |
Default injection mode. |
target |
string | "body" |
Default target CSS selector. |
source |
string | "body" |
Default source CSS selector. |
title |
string | "title" |
Title selector ("selector" or "selector/attribute"). |
scroll |
bool|null | null |
Scroll behavior. null = auto (depends on mode and context). |
urlPrefix |
string|null | null |
Prefix added to fetched URLs. |
progress |
bool | true |
Show progress bar during fetch. |
prefetch |
bool | true |
Prefetch pages on link hover. |
prefetchTtl |
number | 3000 |
Prefetch cache TTL in milliseconds. |
morph |
bool | true |
Enable DOM morphing (requires idiomorph or custom morph function). |
transition |
bool | true |
Enable View Transitions API. |
errorFallback |
bool | true |
Native navigation on failure, display of error responses to forms. |
confirmQuitText |
string | "Are you sure you want to leave this page?" |
Quit-page confirmation message. |
// Load a page programmatically
mu.load("/page", { history: false, target: "#content" });
// Get the last URL loaded by µJS
mu.getLastUrl(); // "/about" or null
// Get the previous URL
mu.getPreviousUrl(); // "/" or null
// Enable/disable quit-page confirmation
mu.setConfirmQuit(true);
mu.setConfirmQuit(false);
// Register a custom morph function
mu.setMorph(function(target, html, opts) {
myMorphLib.morph(target, html, opts);
});µJS works in all modern browsers. The minimum versions are determined by AbortController (used for request cancellation).
Desktop:
| Browser | Version | Release date |
|---|---|---|
| Chrome | 66+ | April 2018 |
| Edge | 79+ | January 2020 |
| Firefox | 57+ | November 2017 |
| Safari | 12.1+ | March 2019 |
| Opera | 53+ | May 2018 |
Mobile:
| Browser | Version | Release date |
|---|---|---|
| Chrome Android | 66+ | April 2018 |
| Safari iOS | 11.3+ | March 2018 |
| Firefox Android | 57+ | November 2017 |
| Opera Mobile | 47+ | May 2018 |
View Transitions require Chrome/Edge 111+. On unsupported browsers, transitions are skipped silently.
DOM morphing requires a separate library (idiomorph recommended). Without it, µJS falls back to direct DOM replacement.
µJS does not support Internet Explorer or legacy Edge (EdgeHTML).