Javascript Browser History go() Method

Introduction

Click on the button to go back two pages:

View in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="goBack()">Go 2 pages back</button>

<script>
function goBack() {//  w w w. ja va 2 s. co  m
  window.history.go(-2);
}
</script>

</body>
</html>

The go() method loads a specific URL from the history list.

history.go(number|URL);

Parameter Values

Parameter
Description
number|URL
Required. where to go

The parameter can either be a number or a URL String.

If the parameter is a number, it goes to the URL within the specific position.

-1 goes back one page, 1 goes forward one page.

If the parameter is a String, the string must be a partial or full URL.

The go() method will go to the first URL that matches the string.




PreviousNext

Related