HomeAbout

History

What is history

Provides access to the browser's session history through the history global object.

It exposes useful methods and properties that let you navigate back and forth through the user's history, and manipulate the contents of the history stack.

The back and forward methods work exactly like the Back and Forward buttons on browser toolbar GUI.

history.back(); history.forward();

You can jump to any part of the history with go() method.

  • identified by its relative position to the current page.
history.go(-1); // same as back history.go(1); // same as forward

You can refresh the page with 0 or no arguments passed to go() method:

history.go(); // refersh current page history.go(0);

Viewing number of pages in the history stack:

const numberOfEntries = history.length;

Pushing state to a History

const state = { page_id: 1, user_id: 5 }; const url = "hello-world.html"; history.pushState(state, "", url);

Changing Query Parameter

const url = new URL(location); url.searchParams.set("foo", "bar"); history.pushState({}, "", url);
AboutContact