History Object-tutorial
In JavaScript, the history object is part of the Browser Object Model (BOM) and gives access to the user's browsing history within the current browser tab or window.
It allows developers to programmatically navigate backward, forward, or jump to specific pages in the session history, enhancing user navigation without reloading the entire page.
Key Property
history.length
Returns the total number of URLs in the browsing history list of the current session.
Example:
Returns the total number of URLs in the browsing history list of the current session.
Example:
console.log(history.length);
Methods of the History Object
history.back()
Navigates to the previous page (similar to clicking the browser’s “Back” button).
history.back()
Navigates to the previous page (similar to clicking the browser’s “Back” button).
history.back();
history.forward()
Navigates to the next page (similar to clicking the browser’s “Forward” button).
Navigates to the next page (similar to clicking the browser’s “Forward” button).
history.forward();
history.go(delta)
Navigates to a specific page relative to the current page by the number of steps defined in delta.
- history.go(-1) is the same as history.back().
- history.go(1) is the same as history.forward().
history.go(-2); // Moves two pages back