JavaScript Window History
The window.history
object contains the browser's history.
Window History
The window.history object can be written without the window
prefix.
To protect user privacy, restrictions have been imposed on JavaScript's access to this object's methods.
Some methods:
history.back()
- Same as clicking the back button in the browserhistory.forward()
- Same as clicking the forward button in the browser
Window history.back()
The history.back()
method loads the previous URL in the history list.
This is the same as clicking the back button in the browser:
Example
Create a back button on the page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function goBack() {
window.history.back()
}
</script>
</head>
<body>
<input type="button" value="Back" onclick="goBack()">
</body>
</html>
The above code outputs:
function goBack() {
window.history.back()
}
Window history.forward()
The history.forward()
method loads the next URL in the history list.
This is the same as clicking the forward button in the browser:
Example
Create a forward button on the page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function goForward() {
window.history.forward()
}
</script>
</head>
<body>
<input type="button" value="Forward" onclick="goForward()">
</body>
</html>
The above code outputs:
function goForward() {
window.history.forward()
}