如何使用JavaScript清除浏览历史记录

How to clear browsing history using JavaScript?

本文关键字:历史 记录 浏览 清除 何使用 JavaScript      更新时间:2023-09-26

我正在做一个简单的项目,让我们以高安全性的网站为例。我有 5 个不同的 JSP 页面。如果我从第一个 JSP 页面开始,它会重定向到第二个 JSP 页面,依此类推。同时,它不应该将这些页面存储在我的浏览器历史记录中。如何使用JavaScript清除这些浏览历史记录?

您可以尝试使用document.location.replace()它用于清除历史记录中的最后一个条目并将其替换为新 url 的地址。 replace()从文档历史记录中删除当前文档的 URL,这意味着无法使用"后退"按钮导航回原始文档。

<script type="text/javascript">
    function Navigate(){   
         window.location.replace('your link');
        return false;
    }
   </script>

.HTML:

   <button onclick="Navigate()">Replace document</button>

正如 MDN Window.history(( 所描述的:

对于顶级页面,您可以在浏览器后退和前进按钮旁边的下拉列表中查看会话历史记录中的页面列表,可通过 History 对象访问。

出于安全原因,History 对象不允许非特权代码访问会话历史记录中其他页面的 URL,但它允许它导航会话历史记录。

无法清除会话历史记录或禁用非特权代码的后退/前进导航。最接近的解决方案是 location.replace(( 方法,该方法将会话历史记录的当前项替换为提供的 URL。

所以没有 Javascript 方法来清除会话历史记录,相反,如果你想阻止导航回某个页面,你可以使用 location.replace(( 方法,并将页面链接作为参数传递,这样不会将页面推送到浏览器的会话历史记录列表中。例如,有三个页面:

答.html:

<!doctype html>
<html>
    <head>
        <title>a.html page</title>
    <meta charset="utf-8">
    </head>
    <body>
         <p>This is <code style="color:red">a.html</code> page ! Go to <a href="b.html">b.html</a> page !</p>        
    </body>
 </html>

乙.html:

<!doctype html>
<html>
    <head>
    <title>b.html page</title>
    <meta charset="utf-8">
</head>
<body>
    <p>This is <code style="color:red">b.html</code> page ! Go to <a id="jumper" href="c.html">c.html</a> page !</p>
    <script type="text/javascript">
        var jumper = document.getElementById("jumper");
        jumper.onclick = function(event) {
            var e = event || window.event ;
            if(e.preventDefault) {
                e.preventDefault();
            } else {
                e.returnValue = true ;
            }
            location.replace(this.href);
            jumper = null;
        }
    </script>
</body>

c.html:

<!doctype html>
<html>
<head>
    <title>c.html page</title>
    <meta charset="utf-8">
</head>
<body>
    <p>This is <code style="color:red">c.html</code> page</p>
</body>
</html>

使用 href 链接,我们可以从 a.html 导航到 b.html再到 c.html。在 b.html 中,我们使用 location.replace(c.html) 方法从 b.html 导航到 c.html。最后,我们转到 c.html*,如果我们单击浏览器中的后退按钮,我们将跳转到 **a.html

原来如此!希望对您有所帮助。

没有插件就无法清除用户历史记录。而且从开发人员的角度来看,这不是问题,而是用户清除历史记录的负担。

有关信息,请参阅如何使用JavaScript或Java清除浏览器(IE,Firefox,Opera,Chrome(历史记录,除了浏览器本身?

,这将是一个安全问题。


但是,可以在Google chrome扩展程序中清除JavaScript中的历史记录。chrome.history.deleteAll((.
window.location.replace('pageName.html');

与 HTTP 重定向类似的行为

阅读 如何在 JavaScript/jQuery 中重定向到另一个网页?

禁用后退按钮的后退功能:

window.addEventListener('popstate', function (event) {
  history.pushState(null, document.title, location.href);
});

好的。这是一段古老的历史,但可能是我的解决方案可能对您或其他开发人员有用。如果我不希望用户在页面中按后退键(假设从页面 A 调用页面 B(并返回到最后一页(页面 A(,我会执行以下步骤:

首先,在 A 页上,而不是使用 window.location.hrefwindow.location.replace 调用下一页,我使用两个命令进行调用:window.open 和 A 页上window.close示例:

<a href="#"
onclick="window.open('B.htm','B','height=768,width=1024,top=0,left=0,menubar=0,
         toolbar=0,location=0,directories=0,scrollbars=1,status=0');
         window.open('','_parent','');
         window.close();">
      Page B</a>;

窗口打开时的所有修饰符只是为了组成结果页面。这将打开一个新窗口(popWindow(,而无法使用后退键,并将关闭调用方页面(页面A(

第二:在B页上,如果您希望此页面执行相同的操作,则可以使用相同的过程。

井。这需要用户接受您可以打开弹出窗口,但是在受控系统中,就像您正在为您的工作或客户编程页面一样,这很容易推荐给用户。只需接受受信任的网站即可。

您无法清除浏览器历史记录。它属于用户,而不是开发人员。另请查看 MDN 文档。

更新:您发布的链接实际上并没有清除您的浏览器历史记录。它只是阻止使用后退按钮。