JavaScript BOM History Object


The History Object

This object provides access to the browser's navigation history, and allows us to navigate back and forth between pages.

Here, are some common methods and properties of the history object.

  1. history.back()
  2. history.forward()
  3. history.go(n)

1. history.back()

This method navigates the browser back one page in the history.

window.history.back();

2. history.forward()

This method navigates the browser forward one page in the history.

window.history.forward();

3. history.go(n)

This method navigates the browser to the page that is n positions away from the current page in the history. A positive value of n moves forward in the history, and a negative value of n moves backward in the history.

window.location.href = "https://tutorialstrend.com/";

Here is an example of comnined all history object methods in JavaScript.

<!DOCTYPE html>
<html>
<head>
<title>Window Example</title>
<style>
button {
background: bisque;
}

</style>
</head>
<body>
<h2>TutorialsTrend - BOM History Examples</h2>
<button onclick="goBack()">Go Back</button>
<button onclick="goForward()">Go Forward</button>
<button onclick="goToPage()">Go to Page</button>

<script>
function goBack() {
window.history.back();
}

function goForward() {
window.history.forward();
}

function goToPage() { 
window.location.href = "https://tutorialstrend.com/";
} 
</script>
</body>
</html>

Output

javascript BOM History

Now you can click on different button and check one by one.


Prev Next