history.go(url/number)

In this chapter you will learn:

  1. How to use go method from history object
  2. How to pass in a URL to history go method

Go to a step

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   j a  va  2s .c  om*/
history.go(-1); 
//go forward one page 
history.go(1); 
//go forward two pages 
history.go(2);
<!DOCTYPE HTML> <!--from   j a  v a  2s .co  m-->
<html> 
    <body> 
        <script> 
            history.go(-1); 


        </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> <!--from   j a  v a 2 s. c o  m-->
<html> 
    <body> 
        <script type="text/javascript"> 
                    window.history.go("http://www.java2s.com"); 
        </script> 
    </body> 
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. How to access the length property in history object
Home » Javascript Tutorial » History
History Object
history.back()
history.forward
history.go(url/number)
history.length
history.pushState