javascript 清除浏览器历史记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5288703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:39:33  来源:igfitidea点击:

Clear browser history

javascript

提问by John

I wrote the following code to clear the browser history and it's working correctly in Internet Explorer but it does not work in Mozilla Firefox. How can I solve this problem?

我编写了以下代码来清除浏览器历史记录,它在 Internet Explorer 中正常工作,但在 Mozilla Firefox 中不起作用。我怎么解决这个问题?

<script language="JavaScript">
function DisablingBackFunctionality()
{
    var URL;
    var i ;
    var QryStrValue;
    URL=window.location.href ;
    i=URL.indexOf("?");
    QryStrValue=URL.substring(i+1);
    if (QryStrValue!='X')
    {
        window.location.href="http://localhost:8085/FruitShop/";
    }
}
</script>

I'm writing this code in <header>section.

我正在编写这段代码<header>

回答by mplungjan

  1. Do NOT try to break the back button
  2. Instead on the page you want not to return to use location.replace(url)
  1. 不要试图打破后退按钮
  2. 而是在您不想返回的页面上使用 location.replace(url)

Also your code can be vastly simplified - but be aware it does not CLEARthe history. You cannot clear the history, only keep a page from getting into the history or as you try, break the back button

此外,您的代码可以大大简化 - 但请注意,它不会清除历史记录。您无法清除历史记录,只能防止页面进入历史记录,或者在尝试时打破后退按钮

function DisablingBackFunctionality() {
// get the query string including ?
  var passed =window.location.search; 
// did we receive ?X
  if (passed && passed.substring(1) =="X") { 
// if so, replace the page in the browser (overwriting this page in the history)
    window.location.replace("http://localhost:8085/FruitShop/"); 
  }
}