files/en-us/web/api/history/pushstate/index.md
{{APIRef("History API")}}
The pushState() method of the {{domxref("History")}} interface adds an entry to the browser's
session history stack.
pushState(state, unused)
pushState(state, unused, url)
state
: The state object is a JavaScript object which is associated with the
new history entry created by pushState(). Whenever the user navigates to
the new state, a {{domxref("Window/popstate_event", "popstate")}} event is fired, and
the state property of the event contains a copy of the history entry's
state object.
The state object can be anything that can be serialized.
[!NOTE] Some browsers save
stateobjects to the user's disk so they can be restored after the user restarts the browser, and impose a size limit on the serialized representation of astateobject, and will throw an exception if you pass astateobject whose serialized representation is larger than that size limit. So in cases where you want to ensure you have more space than what some browsers might impose, you're encouraged to use {{domxref("Window.sessionStorage", "sessionStorage")}} and/or {{domxref("Window.localStorage", "localStorage")}}.
unused
url {{optional_inline}}
pushState(), but it may
attempt to load the URL later, for instance, after the user restarts the browser. The
new URL does not need to be absolute; if it's relative, it's resolved relative to the
current URL. The new URL must be of the same {{glossary("origin")}} as the current
URL; otherwise, pushState() will throw an exception. If this parameter
isn't specified, it's set to the document's current URL.None ({{jsxref("undefined")}}).
SecurityError {{domxref("DOMException")}}
url parameter is not a valid URL, or if the method is called too frequently.DataCloneError {{domxref("DOMException")}}
state parameter is not serializable.In a sense, calling pushState() is similar to
setting window.location = "#foo", in that both will also create and
activate another history entry associated with the current document.
But pushState() has a few advantages:
window.location = "#foo"; only creates a new history entry if the
current hash isn't #foo.Note that pushState() never causes a {{domxref("Window/hashchange_event", "hashchange")}} event to be
fired, even if the new URL differs from the old URL only in its hash.
This creates a new browser history entry setting the state and url.
const state = { page_id: 1, user_id: 5 };
const url = "hello-world.html";
history.pushState(state, "", url);
const url = new URL(location);
url.searchParams.set("foo", "bar");
history.pushState({}, "", url);
{{Specifications}}
{{Compat}}