How to use go method from history object in Javascript

Description

The go() method can navigate through the user's history backward or forward. go() accepts an integer representing the number of pages to go backward or forward a its single argument.

A negative number moves backward in history and a positive number moves forward.


//go back one page /*from w  w w .  jav a  2  s  .  c om*/
history.go(-1); 
//go forward one page 
history.go(1); 
//go forward two pages 
history.go(2);

Example


<!DOCTYPE HTML> 
<html> 
    <body> 
        <script> 
            history.go(-1); <!-- ww w . ja v  a 2s  .c o m-->


        </script> 
    </body> 
</html>

Click to view the demo

Pass in string parameter

The go() method argument can be a string. The browser navigates to the first location in history that contains the given string.

The closest location may be either backward or forward. If there's no entry in history matching the string, then the method does nothing:


<!DOCTYPE HTML> 
<html> 
    <body> 
        <script type="text/javascript"> 
                    window.history.go("http://www.java2s.com"); 
        </script> 
    </body> 
</html><!--from   w w  w  .j  ava 2  s. c om-->

Click to view the demo





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window