Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS History

The window.history object stores the browsing history of the user’s session.

Window History

The history object can be used without the window prefix.

To safeguard user privacy, JavaScript has restricted access to this object.

Some available methods include:

  • history.back() – functions like pressing the back button in the browser.
  • history.forward() – behaves like clicking the forward button in the browser.

Window History Back

The history.back() method navigates to the previous URL in the history list, similar to clicking the Back button in the browser.

Example

Create a button on a page that navigates back:

<html>
<head>
<script>
function goBack() {
  window.history.back()
}
</script>
</head>
<body>

<input type=”button” value=”Back” onclick=”goBack()”>

</body>
</html>

The result of the code above will be:

Window History Forward

The history.forward() method navigates to the next URL in the history list, similar to clicking the Forward button in the browser.

Example

Create a button on a page that navigates forward:

<html>
<head>
<script>
function goForward() {
  window.history.forward()
}
</script>
</head>
<body>

<input type=”button” value=”Forward” onclick=”goForward()”>

</body>
</html>