Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

javascript - Clear browser history

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?

<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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Do NOT try to break the back button
  2. Instead on the page you want not to return to use location.replace(url)

Also your code can be vastly simplified - but be aware it does not CLEAR the 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/"); 
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...