Home » JavaScript » JavaScript Window History

JavaScript Window History

The window.history object stores an array of URLs visited by the user. All the url's can whenever loaded gets stored in the object which can be used later to navigate.

The window.history object can be written also without the window prefix like all window objects.


History Object Methods

Property Description
history.back() Works as clicking back button in the browser.
history.forward() Works as clicking forward button in the browser.

JavaScript History Back

The history.back() method loads the previous URL in the history list. It allows the user to load whatever the previous loaded page was in the same tab.

Example

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Window Window History </title>
<script>
function goBack() {
window.history.back()
}
</script>
</head>
<body>
<input type="button" value="Back" onclick="goBack()">
</body>
</html>

Output

JavaScript Window Window History

Window History Forward

The history.forward() method loads the next URL in the history list. It allows the user to go to the page from which the user came back by clicking backward button.

Example

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Window History </title>
<script>
function goForward() {
window.history.forward()
}
</script>
</head>
<body>
<input type="button" value="Forward" onclick="goForward()">
</body>
</html>

Output

JavaScript Window History








Follow Us: