Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Window History

JavaScript Window History


The window.history object contains the browser's history.


Window History

window.historyThe object can be used without the window prefix when writing.

To protect user privacy, restrictions have been placed on the methods JavaScript uses to access this object.

Some methods:


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">
<head>
<script>

function goBack() { window.history.back() }

script

head

<body>
<input type="button" value="Back" onclick="goBack()" body html>

The above code outputs:


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: