Window History


The window.history object in JavaScript gives users access to the browser's session history, which records URLs that users have visited while using the window or tab they are currently using. This object, which is a component of the browser object model (BOM), contains methods for navigating through the history, including back(), advance(), and go().

Here are some common methods of the Window.History object:

history.back()

Takes the user back to the previous page of history.

Example

Preview

history.forward()

Takes the user to the next page of history.

Example

Preview

history.go()

Takes the user n pages away from the current page of history. A negative value for n will take the user n pages back, while a positive value will take the user n pages forward.

Example

Preview

history.pushState()

Adds a new entry to the browser's session history with the specified state object, title, and URL. This method doesn't actually navigate to the new URL, but it does allow you to update the URL displayed in the address bar.


history.replaceState()

Replaces the current entry in the browser's session history with the specified state object, title, and URL. This method doesn't create a new entry in the history, but it does allow you to update the URL displayed in the address bar.

The history.pushState() and history.replaceState() methods can be used to replace the URL shown in the address bar, without requiring a full page reload. It can be used to build single-page apps or update URLs to reflect AJAX request-enabled changes.

Be aware that browser security rules include restrictions on how you can use the history object. For example, the script may travel through the user's history only if the user clicked a link or button in the user interface for the first time.