Clear web page history and block the back button!_javascript tips
WBOY
Release: 2016-05-16 18:57:15
Original
2008 people have browsed it
This article introduces various solutions for disabling the browser back button that can be found on the Internet, and analyzes their respective advantages, disadvantages and applicable occasions. 1. Overview Many people have asked, "How can I 'disable' the browser's back button?", or "How can I prevent users from clicking the back button to return to previously visited pages?" "This question is also one of the most frequently asked questions on the ASP forum. Unfortunately, the answer is very simple: we can't disable the browser's back button. At first I was incredulous that someone would want to disable the browser’s back button. Later, I was relieved to see that so many people wanted to disable the back button (the only ones they wanted to disable were the back button, not the browser's forward button). Because by default, after submitting the form, the user can return to the form page through the back button (instead of using the "Edit" button!), and then edit and submit the form again to insert new records into the database. This is something we don’t want to see. So I decided to find a way to avoid this situation. I visited many websites and consulted various implementation methods introduced by these websites. If you frequently visit ASP programming websites, you may have seen some of the content introduced in this article. The task of this article is to introduce all possible methods to everyone, and then find the best method! 2. Disable caching Among the many solutions I found, one of them suggested disabling page caching. Specifically, use a server-side script as follows:
This method is very effective! It forces the browser to revisit the server to download the page instead of reading the page from cache. When using this method, the programmer's main task is to create a session-level variable that determines whether the user can still view the page that is not suitable for access via the back button. Since the browser
no longer caches this page, the browser will re-download the page when the user clicks the back button, and the program can then check that session variable to see if the user should be allowed to open the page. For example, suppose we have the following form:
Response.ExpiresAbsolute = Now() - 1 Response.Expires = 0 Response.CacheControl = "no-cache" If Len(Session( "FirstTimeToPage")) > 0 then &single; The user has visited the current page and is now returning to visit again. &single; Clear session variables and redirect the user to the login page. Session("FirstTimeToPage") = "" Response.Redirect "/Bar.asp" Response.End End If &single; If the program runs here, it means that the user can view Current page &single; The following starts creating the form %>
is submitted (when SompePage.asp is opened), we must give FirstTimeToPage a value. That is, in SomePage.asp we need to add the following code: Session("FirstTimeToPage") = "NO" In this way, if the user who has opened SomePage.asp clicks the back button, the browser The server will be re-requested to download the page. The server checks that Session ("FirstTimeToPage") contains a value, so it clears Session("FirstTimeToPage") and redirects the user to other pages. Of course, all of this requires the user to have cookies enabled, otherwise the session variables will be invalid. (For more explanation of this problem, see For session variables to work, must the Web visitor have cookies enabled?) In addition, we can also use client-side code to prevent the browser from caching Web pages: