Home  >  Article  >  Web Front-end  >  How to disable browser back button in javascript_javascript tips

How to disable browser back button in javascript_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:54:081188browse

1.

Copy code The code is as follows:



Use JS to generate a "forward" action to offset the back function. This method should It is the most concise and does not need to consider the situation where the user clicks "back" twice or multiple times. The disadvantage is that it will become invalid when JavaScript is disabled on the user side.

2.
Copy code The code is as follows:

Use location.replace to go from one page to another. This method 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 this is probably what many people are looking for, but it's still not the best approach in every situation. The disadvantage of this approach is that simply using Response.Redirect will no longer work because every time the user goes from one page to another, we have to clear location.history in client code. Also note that this method clears the last access history record, not all access records.

3.

When the Backspace key is pressed on the keyboard
1. Prohibit the browser from automatically going back
2. However, passwords, single-line text, and multi-line text will not be affected. Rollback operation for input boxes etc.
Copy code The code is as follows:



The above methods are all for In order for the "back" button to react, the client browser needs to turn on JavaScript code.

4. Disable caching
Copy code The 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");
%>

This method uses server-side scripts to force the browser to revisit the server download page without reading it from the cache, and combines the tag in the struts jsp page to implement redirection.

The above methods have certain limitations

5.
Copy code Code As follows:




This method is relatively lazy. Close the browser and reopen it. After my test, there is almost no visual delay. At the same time, it ensures that the back button is unavailable (the back button is gray in new window browsers). It seems to be a good method, but the shortcomings are also obvious:

First of all, the size of the browser window that is closed and reopened may be different, and the user can clearly see this process, and it affects the operation to a certain extent.

Secondly, same as above, this is a JavaScript method.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn