javaScript window history object

the window.history object contains information about the urls(uniform resource locator) visited by the client.

the window.history returns a reference to the history object, which provides an interface for manipulating the browser session history.

the history object is part of the window object and is accessed through the window.history property.

syntax:

          var   historyobj  =   window.history 

note : the window.history object can be written without the window perfix.


history object length property

the history.length property retrieves the number of urls in the history list.

syntax:

          var   list  =  history.length;
   <script>
   
document.write('number of urls evaluates with history object--'+history.length);
   
   </script>

history object back() method

the history.back method is used to loads the previous url in the history list.

this method perform same action as a user clicks the browser's back button.

the back() method works the same as history.go(-1) .

syntax:

          history.back()
   <script>

         function    back_url(){   window.history.back() ; }     </script>
         
  <button   onclick="back_url()" > back url </button>       

history object forward() method

the history.forward method loads the next url from the history list.

this method performs the same action as when a user clicks the forward button in the browser.

the forward() method works the same as history.go(1) .

syntax:

          history.forward() 
   <script>

         function    forward_url(){   window.history.forward() ; }     </script>
         
  <button   onclick="forward_url()" > forward url </button>       

history object go() method

the history.go method loads a specific url from the history list.

syntax:

          history.go(integer  |  stringurl )
  • integer    : the integer indicates the relative position of a url in the history list.
  • stringurl : a string indicates an exact url in the history list.

example: click on this button to go back three pages:

   <script>

         function    go_url(){   window.history.go(-3) ; }     </script>
         
  <button   onclick="go_url()" > go third web page </button>       

example_1 : click on this button to go forward next page:

   <script>

         function    go_url(){   window.history.go(1) ; }     </script>
         
  <button   onclick="go_url()" > go  next web page </button>       

when no any url in history object then the forward() method not work.