How does javascript disable the browser back button

  • 2020-03-30 02:29:50
  • OfStack

1.
 
<script language="JavaScript"> 
javascript:window.history.forward(1); 
</script> 

Using JS to generate a "forward" action to offset the backward function, this method should be the most concise, and do not need to consider the user to connect the point of two or more "backward", the disadvantage is when the user disabled JavaScript that is invalid.

2,
 
<A HREF="logout.do" onclick="javascript:location.replace(this.href); event.returnValue=false; "> 
Logout (Back Disabled) 
</A> 

Use location.replace to go from one page to another. This works by replacing the current history with the URL of the new page, so that there is only one page in the browsing history and the back button never becomes available. I think that's probably what a lot of people are looking for, but it's still not the best approach in any situation. The downside of this approach is that simply using response.redirect is no longer effective, because every time the user goes from one page to another, we have to clear location.history with client code. Also note that this method clears the last access history, not the entire access history.

3,

When the keyboard hits the Backspace key
1, the browser is not allowed to automatically back
2, but does not affect the password, single-line text, multi-line text input box, etc
 
<script type="text/javascript"> 

//Handling keyboard events disables Backspace passwords or single - or multi-line text fields
function banBackSpace(e){ 
var ev = e || window.event;//Get the event object
var obj = ev.target || ev.srcElement;//Get the event source

var t = obj.type || obj.getAttribute('type');//Gets the event source type

//Gets the type of event used as a judgment condition
var vReadOnly = obj.getAttribute('readonly'); 
var vEnabled = obj.getAttribute('enabled'); 
//Handle null value cases
vReadOnly = (vReadOnly == null) ? false : vReadOnly; 
vEnabled = (vEnabled == null) ? true : vEnabled; 

//When the Backspace key is struck, the event source type is password or single-line, multi-line text,
//And if the readonly property is true or the enabled property is false, the backspace key is disabled
var flag1=(ev.keyCode == 8 && (t=="password" || t=="text" || t=="textarea") 
&& (vReadOnly==true || vEnabled!=true))?true:false; 

//When the Backspace key is struck, the Backspace key is invalidated if the event source type is not password or if it is single-line, multi-line text
var flag2=(ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea") 
?true:false; 

// judge  
if(flag2){ 
return false; 
} 
if(flag1){ 
return false; 
} 
} 

//Disable the back button for Firefox and Opera
document.onkeypress=banBackSpace; 
//Disable the back button for IE and Chrome
document.onkeydown=banBackSpace; 

</script> 

In response to the "back" button, the client browser needs to open JavaScript code.

4. Disable caching
 
<% 

response.setHeader("Cache-Control", "no-cache"); 

response.setHeader("Cache-Control", "no-store"); 

response.setDateHeader("Expires", 0); 

response.setHeader("Pragma", "no-cache"); 
%> 

This approach USES server-side scripts to force the browser to revisit the server download page without reading from the cache, combined with the struts JSP page < Logic> Tags implement redirection.

All of the above methods have certain limitations

5,
 
<script language="JavaScript"> 

function logout(){ 

window.close(true); 

window.open("logout.do"); 

} 
</script> 
<button onClick="logout()">Logout</button> 

This method is lazy, close the browser and then open again, after my test in the visual almost no sense of delay, while ensuring that the back button is not available (the back button of the new window browser is gray), it seems a good method, but also more obvious disadvantages:

First, the browser Windows that are closed and reopened may have different sizes, which is obvious to the user and affects the operation to some extent.

Second, as above, this is a JavaScript method.

Related articles: