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

Din*_*mar 19 javascript browser-history

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

Kri*_*h R 27

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

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

HTML:

   <button onclick="Navigate()">Replace document</button>
Run Code Online (Sandbox Code Playgroud)

  • 不是它`window.location.replace()`而不是? (3认同)

San*_*ish 10

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

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

  • 这是一个有用的答案吗?1)安全性始终是开发人员关注的问题,特别是在给出的示例中.2)大致有可能一次解决一个问题,如其他答案所述.3)开发者可能无法阻止恶意登录的嗅探器,但是他们应该尽力清理已经注销或移动但忘记注销的用户. (6认同)

mon*_*jer 10

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

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

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

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

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

a.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>
Run Code Online (Sandbox Code Playgroud)

b.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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

这就是它!希望能帮助到你.


Tus*_*har 5

,那将是一个安全问题。


但是,可以在 Google chrome 扩展程序中清除 JavaScript 中的历史记录。chrome.history.deleteAll()

window.location.replace('pageName.html');
Run Code Online (Sandbox Code Playgroud)

与 HTTP 重定向类似的行为

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

  • *不*清除历史记录是一个安全问题。一般来说,清除历史记录是一个小小的不便。(在眨眼标签和中断因素中的弹出窗口之间的某处) (3认同)