History Object
The history object records the pages (URLs) that the user has browsed, and enables the browser to move forward and backward with similar navigation capabilities.
The moment the window is opened, each browser window, every tab, and even each frame has its own history object associated with a particular window object.
Grammar:
Window.history. [Properties | Method] // window can be omitted.
History Object Properties
History Object method
Using the Length property, the total length of the current window's browsing history, the code is as follows:
1 <script type= "Text/javascript" >2var HL = window.history.length; 3 document.write (HL); 4 </script>
Return to previous browsed page
Back () method to load the previous URL in the History list.
Grammar:
Window.history.back ();
For example, to return to the previous page of the browse, the code is as follows:
Window.history.back (); // Note: The reverse button is equivalent to clicking the browser.
Back () equivalent to go ( -1), the code is as follows:
Window.history.go (-1);
Cases:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">5 <title>Untitled Document</title>6 </Head>7 <Scripttype= "Text/javascript">8 functionGoBack () {9 Window.history.back ();Ten } One </Script> A </Head> - <Body> - Click the anchor link below to add a History list item: the <BR/> - <ahref= "#target1">First Anchor Point</a> - <aname= "Target1"></a> - <BR/> + <ahref= "#target2">A second anchor point</a> - <aname= "Target2"></a> + <BR/><BR/> A using the buttons below, the implementation returns to the previous page: at <form> - <inputtype= "button"value= "Return to previous page"onclick= "GoBack ();" /> - </form> - </Body> - </HTML>
Back to other pages in browsing history
The Go () method loads a specific page in the History list, depending on the page currently in place.
Grammar:
Window.history.go (number);
Parameters:
1---Previous, go (1) equivalent forward ()
0---current page
-1---The Next, go (-1) equivalent back ()
Other values---the relative position of the URL to be accessed in the history's URL list.
In the browser, the second history page that was browsed before the current page is returned, with the following code:
Window.history.go (-2);
and click the Back button two times in the browser.
In the same vein, return to the third history page that you visited after the current page, with the following code:
Window.history.go (3);
Cases:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">5 <title>Untitled Document</title>6 </Head>7 <Scripttype= "Text/javascript">8 functionGoBack () {9 History.go (-1);Ten } One A functionGoForward () { - History.go (2); - } the </Script> - </Head> - <Body> - Click the anchor link below to add a History list item: + <BR/> - <ahref= "#target1">First Anchor Point</a> + <aname= "Target1"></a> A <BR/> at <ahref= "#target2">A second anchor point</a> - <aname= "Target2"></a> - <BR/><BR/> - use the buttons below to return to the previous or next page: - <form> - <inputtype= "button"value= "Return to previous page"onclick= "GoBack ();" /> in <inputtype= "button"value= "Return to next page"onclick= "GoForward ();" /> - </form> to </Body> + </HTML>
JavaScript Advanced--Browser Object-history Object