JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Window History

The window.history object contains the browsers history.


The History Object

The window.history object contains the browser session history, a list of all the pages visited in the current frame or window.

The window.history object can be written without the window prefix.

Some examples:

The following section will show you how to get the information of user's browsing history.

However, To protect the privacy of the users, there are limitations to how JavaScript can access this object.


Getting the Number of Pages Visited

The history.length property returns the number of pages in the session history of the browser for the current window.

It also includes the currently loaded page.

Run code

You can use this property to find out how many pages a user has visited during the current browser session.



Going Back to the Previous Page

The history.back() method loads the previous URL in the history list.

This is the same as clicking the "Back button" in your browser.

Run code

The above code will display the following output:


Going Forward to the Next Page

The history.forward() method loads the next URL in the history list.

This is the same as clicking the "Forward button" in your browser.

Run code

The above code will display the following output (This example will not work if the next page does not exist in the history list):


Going to a Specific Page

You can also load specific page from the session history using the history.go() method.

This method takes an integer as a parameter.

A negative integer moves backward in the history, and a positive integer moves forward in the history.

Run code

The above code will display the following output:

Note: If you attempt to access the page that does not exist in the window's history then the methods history.back(), history.forward() and history.go() will simply do nothing.