Debian Codegraph @

www/src/content/reference/01-attributes/16-hx-replace-url.md

raw symbols SCIP
1 ---
2 title: "hx-replace-url"
3 description: "Replaces the current URL in browser history"
4 ---
5
6 The `hx-replace-url` attribute allows you to replace the current url of the
7 browser [location history](https://developer.mozilla.org/en-US/docs/Web/API/History_API).
8
9 ## Syntax
10
11 ```html
12 <div hx-get="/account" hx-replace-url="true">Go to My Account</div>
13 ```
14
15 The possible values of this attribute are:
16
17 1. `true`, which replaces the fetched URL in the browser navigation bar.
18 2. `false`, which disables replacing the fetched URL if it would otherwise be replaced due to inheritance.
19 3. A URL to be replaced into the location bar.
20 This may be relative or absolute, as per [
21 `history.replaceState()`](https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState).
22
23 Here is an example:
24
25 ```html
26 <div hx-get="/account" hx-replace-url="true">
27 Go to My Account
28 </div>
29 ```
30
31 This will cause htmx to snapshot the current DOM to `localStorage` and replace the URL `/account' in the browser
32 location bar.
33
34 Another example:
35
36 ```html
37 <div hx-get="/account" hx-replace-url="/account/home">
38 Go to My Account
39 </div>
40 ```
41
42 This will replace the URL `/account/home' in the browser location bar.
43
44 ## Notes
45
46 * The [`HX-Replace-Url` response header](/reference/headers/HX-Replace-Url) has similar behavior and can override this attribute.
47 * The [`hx-push-url` attribute](/reference/attributes/hx-push-url) is a similar and more commonly used attribute, which creates a
48 new history entry rather than replacing the current one.