JavaScript Window History
The browsers keep track of the history using the window.history
object.
Window History
You can write the history
object without using the "window" prefix.
To safeguard users' privacy, there are restrictions on how JavaScript can interact with this object.
Some methods:
history.back()
works just like pressing the back button in the browser.history.forward()
- does the same thing as clicking the forward button in the browser.
Window History Back
The history.back()
function brings up the webpage that was visited before the current one.
This is just like pressing the Back button in your web browser.
Example
Create a back button on a 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 code above will be:
Window History Forward
The history.forward()
function is used to open the next web address in the browsing history.
This is like pressing the Forward button in the web browser.
Example
Create a forward button on a 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 code above will be: