清除浏览器历史记

Joh*_*ohn 2 javascript

我编写了以下代码来清除浏览器历史记录,它在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>
Run Code Online (Sandbox Code Playgroud)

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

mpl*_*jan 7

  1. 不要试图打破后退按钮
  2. 而是在您不希望返回的页面上使用location.replace(url)

您的代码也可以大大简化 - 但要注意它不会清除历史记录.您无法清除历史记录,只保留页面进入历史记录或在尝试时打破后退按钮

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/"); 
  }
}
Run Code Online (Sandbox Code Playgroud)