Java Script


JavaScript Tutorial


Admission Enquiry Form


History object in JavaScript

In JavaScript, object is the container of properties/variables and methods/functions. History is the object which contains some methods to perform some browser history related functions.

History object properties and methods in JavaScript.

history.length property in JavaScript

The length property returns the number of URLs in the history list of the current browser window.

The property returns at least 1, because the list includes the currently loaded page.

 This property is useful to find out how many pages the user has visited in the current browsing session.

var len=history.length;
document.write(len);



Output:

The code above will display :

1

Note: Internet Explorer and Opera start at 0, while Firefox, Chrome, and Safari start at 1.



history.back() method in JavaScript

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

window.history.back();




history.forward method in JavaScript

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

window.history.forward();


Note: forward() method will not work if the next page does not exist in the history list.


history.go() method in JavaScript

The go() method loads a specific URL from the history list.

window.history.go(-3);


Output:

This will go back to three previous pages in the history list