-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
The JavaScript History Object is part of the Browser Object Model (BOM). It allows your website to interact with the browser’s history — like moving forward, going back, or jumping to a specific page in the history list.
In simple words, it helps you control navigation without requiring the user to click the back or forward buttons.
The history object is a property of the window object, so you can access it as window.history or just history.
It represents the list of pages the user has visited in the current tab.
console.log(history);
console.log(window.history); // Both are the same
The history object is useful when you want to:
Go back to the previous page.
Move forward to the next page.
Navigate multiple pages in the history list.
Create single-page applications (SPA) navigation without full page reload.
The history object has one commonly used property:
| Property | Description |
|---|---|
history.length |
Returns the total number of pages in the session history. |
console.log("Number of pages in history:", history.length);
history.lengthincludes the current page.
The history object has three main methods:
Goes to the previous page in the history.
// Go back one page
history.back();
Equivalent to clicking the browser’s back button.
Goes to the next page in the history.
// Go forward one page
history.forward();
Equivalent to clicking the browser’s forward button.
Goes to a specific page in history using an index.
// Go back 2 pages
history.go(-2);
// Go forward 1 page
history.go(1);
// Reload the current page
history.go(0);
Negative numbers go back in history.
Positive numbers go forward in history.
0 reloads the current page.
You can create back and forward buttons for your website:
<button onclick="history.back()">Go Back</button>
<button onclick="history.forward()">Go Forward</button>
<button onclick="history.go(-2)">Go Back 2 Pages</button>
<button onclick="history.go(1)">Go Forward 1 Page</button>
The first button moves back one page.
The second button moves forward one page.
The third button goes back two pages.
The fourth button moves forward one page.
You can check how many pages the user has visited in the current tab:
console.log("History length:", history.length);
if(history.length > 1){
alert("You have visited multiple pages in this tab.");
} else {
alert("You are on the first page of this tab.");
}
This can help decide whether to show a back button or not.
In SPAs, developers often use history.pushState() and history.replaceState() to change the URL without reloading the page.
// Change the URL without reloading
history.pushState({page: 1}, "Title 1", "?page=1");
history.pushState({page: 2}, "Title 2", "?page=2");
// Replace current state
history.replaceState({page: 3}, "Title 3", "?page=3");
pushState() adds a new entry to history.
replaceState() replaces the current history entry.
This is widely used in modern web apps to update URLs dynamically while keeping users on the same page.
You can detect when the user navigates using the back or forward buttons using the popstate event:
window.addEventListener("popstate", function(event){
console.log("Location changed:", document.location.href);
console.log("State object:", event.state);
});
The popstate event triggers when history changes via back, forward, or go().
The event.state contains the state object you passed to pushState() or replaceState().
You cannot read the full history for privacy reasons — only the number of entries (history.length).
You cannot access pages in other tabs or windows.
Using history.back() or history.forward() depends on user history — if no pages exist, nothing happens.
pushState() and replaceState() only work for modern browsers.
Use history navigation carefully, don’t force users back and forth unexpectedly.
For SPAs, always update state objects with pushState() to track navigation.
Use popstate events to handle custom actions when users navigate.
Always test history functions in multiple browsers to avoid unexpected behavior.
The JavaScript History Object allows you to control the browser’s history. With it, you can:
Go back or forward in the browser history.
Jump to a specific page using history.go().
Track the number of pages in the session.
Dynamically change URLs without reloading pages (SPAs).
Respond to navigation events using popstate.
Mastering the history object is important for building dynamic, user-friendly web applications and controlling page navigation smoothly.
How can you go back one page in the browser history using the history object?
Write a script to move forward one page in history when a button is clicked.
How can you navigate back two pages and forward one page using history.go()?
How can you check how many pages are in the current tab’s history?
Write code to reload the current page using the history object.
How can you add a new entry to the browser history without reloading the page using pushState()?
How can you replace the current history entry with a new URL using replaceState()?
How can you listen for back and forward navigation using the popstate event and log the new URL?
Write a program that shows an alert if the user tries to go back but there is no previous page.
Create a set of buttons that navigate backward, forward, and reload the page using history methods.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
