JavaScript Window History

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

Window History

window.history The object can be written without 'window'.

To protect user privacy, JavaScript has restrictions on accessing this object.

Some methods:

  • history.back() - Equivalent to clicking the back button in the browser
  • history.forward() - Equivalent to clicking the forward button in the browser

Window History Back

history.back() Method loads the previous URL in the history list.

This is equivalent to clicking the back button in the browser.

Example

Create a back button on the page:

<html>
<head>
<script>
function goBack() {
    window.history.back()
 }
</script>
</head>
<body>
<input type="button" value="Back" onclick="goBack()">
</body>
</html>

The output of the above code will be (click this button):


Window History Forward

history forward() Method loads the next URL in the history list.

This is equivalent to clicking the forward button in the browser.

Example

Create a forward button on the page:

<html>
<head>
<script>
function goForward() {
    window.history.forward()
 }
</script>
</head>
<body>
<input type="button" value="Forward" onclick="goForward()">
</body>
</html>

The output of the above code will be: