Moving Forward and Back

It seems logical; if you have access the history object's URLs, then you should be able to make any of the documents represented by the URLs appear in any window or frame. Indeed, you can! The history object provides two methods -- history.forward() and history.back() -- to perform exactly that function. You'll use those methods to create navigation buttons in this exercise.

Forward and Back Buttons

Navigating with JavaScript Buttons

With the following two lines of code, JavaScript attaches history methods to buttons:

When clicked, the button defined by the top line sends the user back a step in his or her History list. The bottom line does the opposite, sending the user forward a step, if possible. You can't see what the pages ahead and behind are, but you can send the browser to them. The History object also has a third method: go(). You can use the go() method to move the browser forward or back a certain number of steps in the History list, like this: history.go(3); history.go(-2); For the go() method, positive numbers represent moves forward in the list, negative numbers represent backward moves. A call to go(0) is a reload of the current page.

Insider: How the basic tech behind the Internet works
Join the discussion
Be the first to comment on this article. Our Commenting Policies