Access Browser History
The browser’s history is represented in JavaScript by the window.history object, which is essentially a list of the URLs previously visited. Its methods enable you to use the list, but not to manipulate the URLs explicitly.
The only property owned by the history object is its length. You can use this property to find how many pages the user has visited:
alert("You've visited " + history.length + " web pages in this browser session");
The history object has three methods.
forward() and back() are equivalent to pressing the Forward and Back buttons on the browser; they take the user to the next or previous page in the history list.
history.forward();
There is also the method go, which takes a single parameter. This can be an integer, positive or negative, and it takes the user to a relative place in the history list:
history.go(-3); // go back 3 pages history.go(2); // go forward 2 pages
The method can alternatively accept a string, which it uses to find the first matching URL in the history list:
history.go("example.com"); // go to the nearest URL in the history // list that contains 'example.com'