BOM History (Window History)

The window.history object contains the browsers history.


Window History

The window.history object can be written without the window prefix.

To protect the privacy of the users, there are ample number of limitations to how JavaScript can access this object.

Some below methods can be used to get window history:

  1. history.back() – same as clicking back in the browser
  2. history.forward() – same as clicking forward in the browser

Window History Back

The history.back() method use to load the previous URL in the history list.

This can be achieved by clicking the Back button in the browser.

Input:-
<html>
<head>
<script>
function goBackPage() {
  window.history.back()
}
</script>
</head>
<body>

<input type="button" value="Click to Back" onclick="goBackPage()">

</body>
</html>

Output (Before Click):-

Output (After Click will take you to the last link whatever it was.)


Window History Forward

The history.forward() method used to load the next URL in the history list.

This can be achieved by clicking the Forward button in the browser.

Input:-
<html>
<head>
<script>
function goForwardPage() {
  window.history.forward()
}
</script>
</head>
<body>

<input type="button" value="Click to Forward" onclick="goForwardPage()">

</body>
</html>

Output (Before Click):-

Output (After Click will take you to the forward link whatever it was.)