Using the go , forward , and back Methods


Using the go , forward , and back Methods

It's easy to put the methods of the history object to work. In fact, we've already seen how to do that in Chapter 4. In that example (Listing 04-05.html on the web site), I used the back , forward , and go methods to navigate forward and backward in the browser's history. Here's what the code looks like:

 <HTML>      <HEAD>          <TITLE>Using the history object</TITLE>      <SCRIPT LANGUAGE="JavaScript">          <!--  function back()   {   window.history.back()   }   function forward()   {   window.history.forward()   }   function back2()   {   window.history.go(-2)   }   function forward2()   {   window.history.go(2)   }  // -->      </SCRIPT>      </HEAD>      <BODY>          <H1>Using the history object</H1>          <FORM>              <BR>              Click a button to go forward or back one page:              <BR>  <INPUT TYPE = BUTTON VALUE = "< Go back" ONCLICK = "back()">   <INPUT TYPE = BUTTON VALUE = "Go forward >" ONCLICK = "forward()">  <BR>              <BR>              Click a button to go forward or back two pages:              <BR>  <INPUT TYPE = BUTTON VALUE = "<< Go back 2" ONCLICK = "back2()">   <INPUT TYPE = BUTTON VALUE = "Go forward 2 >>" ONCLICK = "forward2()">  </FORM>      </BODY>  </HTML> 

You can see this code at work in Chapter 4 in Figure 4.6. Clicking the buttons in this example is like clicking the browser's forward and back buttonsexcept that we're also giving the user the option of moving two pages in either direction.

That's it! That completes our work with the navigator , location , and history objects in this chapter. As you can see, these objects give you control over the inner workings of the browser, determining browser type and version, navigating to new URLs, and moving forward and backward in the history list. In the next chapter, we'll leave the browser objects and start working with the HTML elements in JavaScript, starting with the HTML text elements.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net