/

HTML / CSS / JavaScript Tutorial

JavaScript History method: go()

[this page | pdf | back links]

The go() method (when applied to History objects in the JavaScript BOM) loads a URL from the history list.

 

It has the following syntax with the following parameters. It does not return a value.

 

history.go(param)

 

Parameter

Required / Optional

Description

param

Required

Either a number indicating where within the history list to go to (e.g. -1 goes back a page, +1 goes forward a page), or a string representing a partial or full URL (method will go to first URL that matches the string)

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse :collapse;}
</style>
</head>
<body>
Note: clicking on any of these buttons is unlikely to result in any action,
as there is no previous or subsequent URL in the URL history list<br><br>

<button onclick="historyBack()">go back a page in history list</button><br>
<button onclick="historyForward()">go back a page in history list</button><br>
<button onclick="historyGo()">go back two pages in history list</button><br>

<script>
function historyBack() {
  window.history.back();
}
function historyForward() {
  window.history.forward();
}
function historyForward() {
  window.history.go(-2);
}
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile