瀏覽器瀏覽記憶(history)幾則技巧記錄


一般瀏覽記錄模式

假設有三個頁面, start.html, 通過點擊start.html上的鏈接跳轉到 first.html, 然后點擊first.html上鏈接跳轉到 second.html, 那么在history中記錄的歷史如下鏈表:

 

 

如下代碼例子, 頁面跳轉均以 鏈接實現。

start.html

<html>
<head> 
        <style>

        </style>
</head> 
<body>
         <a href="./first.html">go to first html</a>
</body>
</html>

first.html

<html>
<head> 
        <style>

        </style>
</head> 
<body>
         <a href="./second.html">go to second html</a>
</body>
</html>

second.html

<html>
<head> 
        <style>

        </style>
</head> 
<body>
         here is second html
</body>
</html>

 

某個頁面被瀏覽過后,在history中被替換

對於一種特殊場景, 此頁面用戶看完后, 不想讓用戶再看到, 例如特定頁面上,讓用戶選擇一個條件,此選擇是一次性的, 不能讓用戶多次選擇, 點擊回退按鈕不顯示此頁面.

使用 location.replace接口, 實現代碼如下, first.html頁面點擊連接后, 跳轉到second.html,  first.html頁面在history中被second.html代替。

瀏覽到 first.html history記錄

從first.html跳轉到 second.html后, history記錄

這樣, 當前顯示second,點擊回退按鈕, 回退到start頁面。

實例代碼,其他兩個不變。

first.html

<html>
<head>
        <style>

        </style>
</head>
<body>
     <a id="go2scond" href="#" onclick="go2second();">go to second html</a>
         <script type="text/javascript">
            function go2second()
            {
                location.replace("./second.html");
            }
         </script> 
</body>
</html> 

 

某個頁面之前的所有頁面不能再被瀏覽

如果某個頁面之前的頁面是一次性顯示, 對於用戶來說不能再被顯示, 例如當用戶選擇了所以后的內容后,在最后一步確認后, 不能再被修改, 用戶企圖通過瀏覽器回退按鈕重新查看之前的頁面, 這中條件下, 想法讓其失效。

 

history對象, 沒有提供清空之前瀏覽頁面的接口, 此需求可以通過js腳本判斷實現:

例如下面demo, 用戶從start.html瀏覽到 first.html頁面,后瀏覽到second.html,  然后用戶點擊回退仍然顯示 second.html

修改first.html代碼如下

具體實現原理是, 在之前不能瀏覽頁面中的最后一一個頁面腳本中,添加history.forward(1), 讓其向后自動跳轉到后面一個頁面, 對於第一次訪問此頁面, 由於沒有后面一個頁面, 所以不會執行, 當瀏覽到后一個頁面后, 這時此頁面在history中就有了后一個頁面, 通過后退按鈕, 當加載此頁面時候, 就會自動跳轉到后一個頁面。

<html>
<head>
        <style>

        </style>
</head>
<body>
         <a href="./second.html">go to second html</a>
            <script>
            //HTML禁用回退上一瀏覽頁面按鈕
            /*
            加入有以上代碼的頁面無法回退到它
                例如A頁面有這段代碼,從A鏈接到B頁面,這時一旦在B頁面上按回退按鈕,則無法回退到A頁面
            */
            if(window.history.forward(1) != null)
            {

            }

            </script>

</body>
</html> 

 

瀏覽歷史(前進 和 后退)與刷新的差別

瀏覽歷史(前進和后退),僅僅加載已經訪問過頁面的 URL, 如果其中有一個頁面是以 form 表單 post 方法提交后顯示的, 瀏覽歷史 也僅僅是加載 此頁面的url, 並不提交 post的數據。

刷新, 如果有個頁面是以提交數據顯示的, 則刷新會提示用戶,是否要重復提交數據:

 

demo測試代碼:

start.php form提交數據到 first.php

<html>
<head> 
        <style>

        </style>
</head> 
<body>
    <form action="./first.php" method="post">
        <input type="text" name="para" id="para" value="para" />
        <input type="submit" value="submit" />
    </form>
</body>
</html>

first.php是被提交數據后顯示的, 使用刷新則會提示是否要重復提交數據。點擊回退和前進, 都沒有重復提交數據提示。

<html>
<head> 
        <style>

        </style>
</head> 
<body>
         <a href="./second.php">go to second html</a>
</body>
</html>

second.php

<html>
<head> 
        <style>

        </style>
</head> 
<body>
         here is second html
</body>
</html>

 

通過改造, 在first.php中添加 302 跳轉, 直接跳轉到 second, 可以避免 用戶看到數據提交的頁面 first.php, 同時嘗試前進和后退按鈕, 也不能瀏覽到first.php,說明在history中不會記錄跳轉的中間響應(從應用上也不會記錄, 因為如果記錄, 則回退按鈕則永遠不能回到start.php頁面, 給用戶帶來不便)。

<?php   
//重定向瀏覽器   
header("Location: /replaceTest/second.php");   
//確保重定向后,后續代碼不會被執行   
exit;  
?>    

<html>
<head> 
        <style>

        </style>
</head> 
<body>
         <a href="./second.php">go to second html</a>
</body>
</html>

 

history中記錄的鏈表同replace

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM