當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML DOM History go()用法及代碼示例


HTML中的History go()方法用於從曆史記錄列表中加載特定的URL。如果已知要從曆史記錄中加載的特定頁麵的數字或URL,則它是history.back()和history.forward()方法的更好替代。

用法:

history.go( number|URL )

參數:此方法接受單個參數number | URL,該參數是必需的,用於指定曆史記錄列表中URL的編號/位置或頁麵的URL。 -1用於向後翻一頁,而1用於向後翻一頁。


下麵的程序演示了HTML中的History go()方法:

例:

<!DOCTYPE html> 
<html> 
    <head>  
        <title>DOM History.go() Method</title>  
        <style>  
            h1 {  
                color:green;  
            }  
            h2 { 
                font-family:Impact; 
            } 
            body {  
                text-align:center;  
            }  
        </style>  
    </head> 
    <body> 
        <h1>GeeksforGeeks</h1>  
        <h2>DOM History.go( ) Method</h2>  
        <p> 
         For going to the second next URL in the 
         history, double click the "Go to the  
         second next URL" button:
        </p> 
        <button ondblclick="history_goforward()"> 
          Go to the second next URL 
        </button> 
        <script> 
            function history_goforward() { 
                window.history.go(2);  
            } 
        </script> 
    </body> 
</html>                    

輸出:

範例2:用於轉到曆史記錄列表中的上一個URL。

<!DOCTYPE html> 
<html> 
    <head>  
        <title>DOM History.go() Method</title>  
        <style>  
            h1 {  
                color:green;  
            }  
            h2 { 
                font-family:Impact; 
            } 
            body {  
                text-align:center;  
            }  
        </style>  
    </head> 
    <body> 
        <h1>GeeksforGeeks</h1>  
        <h2>DOM History.go() Method</h2>  
        <p> 
         For going to the previous URL in the 
         history, double click the "Go to the  
         previous URL" button:
        </p> 
        <button ondblclick="history_goback()"> 
         Go to the previous URL 
        </button> 
        <script> 
            function history_goback() { 
                window.history.go(-1);  
            } 
        </script> 
    </body> 
</html>                    

輸出:

支持的瀏覽器:下麵列出了History back()方法支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • 火狐瀏覽器
  • Opera
  • 蘋果瀏覽器


相關用法


注:本文由純淨天空篩選整理自Shubrodeep Banerjee大神的英文原創作品 HTML | DOM History go() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。