JavaScript Window History


The window.history object contains the browser's history.


Window History

window.history objects can be written without the window prefix.

To protect user privacy, JavaScript restricts the way JavaScript accesses the object.

Some methods:

  • history.back() - same as clicking the back button in the browser

  • history.forward() - same as clicking the forward button in the browser


Window history.back()

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

This is the same as clicking the back button in the browser:

Example

To create a back button on the page:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<head>
<script>
function goBack()
{
     window.history.back()
}
</script>
</head>
<body>
 
<input type="button" value="Back" onclick="goBack()">
 
</body>
</html>

Window history.forward()

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

This is the same as clicking the forward button in the browser:

Example

Create a forward button on the page:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function goForward()
{
     window.history.forward()
}
</script>
</head>
<body>
 
<input type="button" value="Forward" onclick="goForward()">
 
</body>
</html>