JavaScript Tutorial/History/History

Материал из Web эксперт
Версия от 08:24, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

History

The History object allows you to navigate through the history of Websites that a browser has displayed.

The browser stores a history of visited URLs in a list, which the History object references.

Properties and Methods of the History Object

Property Description back() Loads the URL for the previous visited Web site current Refers to the current URL in the history list forward() Loads the next URL in the history list go() Loads a URL from the history list length Returns the number of entries in the history list next Refers to the next URL in the history list previous Refers to the previous URL in the history list



<html>
    <head>
    <title> Using the history object to view a list of the browser history</title>
    <script language="JavaScript">
    <!--
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    -->
    
    </head>
    <body>
    <script language="JavaScript">
    <!--
    for (i=0; i<history.length; i++){
       document.writeln(window.history.previous);
    }
    //End Hide -->
    </script>
    </body>
    </html>


History.back()

The back() method is used to load the URL for the previously visited Web site.



<html>
    <head>
    <title> Using the back method of the History object</title>
    </head>
    <body>
    <form name=form1>
    Click on the button to go back to the previous page.
    <input type="button" value="Go Back" onClick="window.history.back()">
    </form>
    </body>
    </html>


History.current

The current property contains a string that specifies the complete URL of the current history entry.



<html>
    <head>
    <title> Using the current property of the History object</title>
    </head>
    <body>
    <script language="JavaScript">
    <!--
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    -->
    </script>
    <form name=form1>
    <input type="button" value="Get Current" onClick="alert(window.history.current)">
    </form>
    </body>
    </html>


History.forward()

The forward() method is used to load the URL for the next Web site in the history list.



<html>
    <head>
    <title> Using the forward method of the History object</title>
    </head>
    <body>
    <form name=form1>
    Click on the button to go to the forward browser page.
    <input type="button" value="Go Forward" onClick="window.history.forward()">
    </form>
    </body>
    </html>


History.go()

Syntax



history.go(num)


History.go(-2)

<html>
<head>
<title>History</title>
</head>
<body>
<h1>history object</h1>
<a href="" onclick="history.back();return false">history.back()</a><br />
<a href="" onclick="history.go(-2);return false">history.go(-2)</a><br /><br />
<a href="" onclick="history.forward();return false">history.forward()</a><br />
</script>
</body>
</html>


History.length

The length property is used to get the number of URLs in the history list.



<html>
    <head>
    <title> Using the length property of the History object</title>
    </head>
    <body>
    <script language="JavaScript">
    <!--
    var numOfURL = window.history.length;
    document.write("The number of URL"s in the history list is: " + numOfURL);
    -->
    </script>
    </body>
    </html>


History.next

The next property is used to get the URL for the next entry in the history list.



<html>
    <head>
    <title> Using the next property of the History object</title>
    </head>
    <body>
    <script language="JavaScript">
    <!--
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    var nextURL = window.history.next;
    document.write("The next URL in the history list is: " + nextURL);
    -->
    </script>
    </body>
    </html>


History.previous

The previous property is used to get the URL for the previous entry in the history list.



<html>
    <head>
    <title> Using the previous property of the History object</title>
    </head>
    <body>
    <script language="JavaScript">
    <!--
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    -->
    </script>
    <form name=form1>
    <input type="button" value="Get Previous" onClick="alert(window.history.previous)">
    </form>
    </body>
    </html>


Simulated Back and Forward Buttons by using the history object

<html>
<head>
<title>Simulated Back and Forward Buttons</title>
</head>
<body>
<h1>Simulated Back and Forward Buttons</h1>
<P><a href="javascript:history.back()">Back</a> |
<a href="javascript:history.forward()">Forward</a></p>
</body>
</html>