How does javascript disable the browser's back button?

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Script language = "JavaScript">
Javascript: window. history. forward (1 );

JS is used to generate a "Forward" action to offset the Backward function. This method should be the most concise, and you do not need to consider the situation of two or multiple "backward" connections, the disadvantage is that JavaScript is disabled on the client.

Copy codeThe Code is as follows:
<A href = "logout. do" onclick = "javascript: location. replace (this. href); event. returnValue = false;">
Logout (Back Disabled)

Use location. replace to switch from one page to another. The principle of this method is to replace the current history with the URL of the new page, so that there is only one page in the history browsing, and the back button will never become available. I think this may be the method that many people are looking for, but it is still not the best method in any situation. The disadvantage of this method is that simply using Response. Redirect will no longer work, because every time a user transfers from one page to another, we must use the client code to clear location. history. Note that this method clears the last access history, not all access records.


After you press the Backspace button on the keyboard
1. Disable automatic playback by the browser
2. Password, single line text, multi-line text input box, and other rollback operations are not affected.
Copy codeThe Code is as follows:
<Script type = "text/javascript">

// Handle Keyboard Events. Do not use Backspace passwords or single-line or multi-line text boxes
Function banBackSpace (e ){
Var ev = e | window. event; // obtain the event object
Var obj = ev.tar get | ev. srcElement; // obtain the event Source

Var t = obj. type | obj. getAttribute ('type'); // obtain the event source type.

// Obtain the event type used as the judgment Condition
Var vReadOnly = obj. getAttribute ('readonly ');
Var vEnabled = obj. getAttribute ('enabled ');
// Process null values
VReadOnly = (vReadOnly = null )? False: vReadOnly;
VEnabled = (vEnabled = null )? True: vEnabled;

// When you press the Backspace key, the event source type is password or single-line or multi-line text,
// If the readonly attribute is true or the enabled attribute is false, the Return key is invalid.
Var flag1 = (ev. keyCode = 8 & (t = "password" | t = "text" | t = "textarea ")
& (VReadOnly = true | vEnabled! = True ))? True: false;

// When you press the Backspace key, the Backspace key is invalid if the event source type is non-Password, single-line, or 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 backend key for Firefox and Opera
Document. onkeypress = banBackSpace;
// Disable the backend key for IE and Chrome
Document. onkeydown = banBackSpace;


The above methods are all for the "back" button reaction, the client browser needs to open JavaScript code.

4. Disable caching
Copy codeThe Code is as follows:

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

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

Response. setDateHeader ("Expires", 0 );

Response. setHeader ("Pragma", "no-cache ");

In this way, server scripts are used to force the browser to re-access the server download page without reading from the cache. Combined with the <logic> tag in the struts jsp page, this method achieves redirection.

The preceding methods have certain limitations.

Copy codeThe Code is as follows:
<Script language = "JavaScript">

Function logout (){

Window. close (true );

Window. open ("logout. do ");

<Button onClick = "logout ()"> Logout </button>

This method is relatively lazy, and the browser is closed and re-opened. After my tests, there is almost no latency visually, at the same time, it ensures that the back button is unavailable (the back button of the browser in the new window is gray). It seems to be a good method, but its disadvantages are also obvious:

First, the size of the closed and re-opened browser windows may be different. You can see that this process affects the operation to a certain extent.

Second, the same as above. This is a JavaScript method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.