JavaScript – History Object


What is a history object in Javascript ?

History object represents the list of URLs loaded in the browser, by the user.

By which we can load the current, previous and forward pages in the browser.

How to access the history object ?

The history object is the window property,

So it can be accessed with or without the ‘window’ prefix.

Syntax :

window.history

or

history

What happens when we invoke history object ?

This object returns the ‘length’ of the URLs loaded in the browser.

Javascript full tutorial !!!

Click to Learn More about – Javascript online learning

What are the methods of history object ?

There are three methods in history object listed below,

1. forward()

forward() method loads the next page.

Example :


<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			history.forward();
		</script>
	</head>
	<body></body>
</html>

Output :

This method loads to the next page

2. back()


back() method loads the previous page.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			history.back();
		</script>
	</head>
	<body></body>
</html>

Output :

This method loads to the previous page.

3. go()

go() method loads the given page number.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			history.go(1);
		</script>
	</head>
	<body></body>
</html>

Output :

This method loads the 1st URL.