Clear the web page history and unmask the button! _ Javascript skills

Source: Internet
Author: User
The browser's back button makes it easy for us to return to previously accessed pages, which is undoubtedly very useful. But sometimes we have to disable this function to prevent users from disrupting the order of page access. This article describes various methods for disabling the browser's back button on the network, analyzes their respective advantages and disadvantages and
Usage.
I. Overview
Many people once asked, "How can we 'deactivate 'the browser's back button ?", Or "how can we prevent users from clicking the back button to return to the previous response ?"
Page ?" This is also one of the most frequently asked questions on the ASP forum. Unfortunately, the answer is very simple: we cannot disable browser fallback.
Button.
At first, I felt incredible that someone wanted to disable the browser's back button. Later, I saw that there were so many people who wanted to disable this and press
Button (only the back button is disabled, not the browser's forward button ). Because by default, after a user submits a form
Go back to the form page (instead of using the "edit" button !), Edit and submit the form again to insert a new record to the database. This is what we do not
Hope to see.
Therefore, I decided to find a way to avoid this situation. I visited many websites and referred to various implementation methods described by these websites. If you
I often visit ASP programming websites. You may have seen some of the content introduced in this article. The task in this article is to introduce all possible methods to you, and then find
The best way!
Ii. Disable caching
Among the many solutions I have found, it is recommended to disable page caching. Specifically, the server script is used as follows:

The Code is as follows:


<%
Response. Buffer = True
Response. ExpiresAbsolute = Now ()-1
Response. Expires = 0
Response. CacheControl = "no-cache"
%>


This method is very effective! It forces the browser to re-access the server download page instead of reading the page from the cache. When using this method, the programmer's master
The task is to create a session-level variable to determine whether the user can still view the page that is not suitable for access by the back button. Because
The browser no longer caches this page. When the user clicks the back button, the browser downloads the page again. Then, the program can check the session variable to see if
Users should be allowed to open this page.
For example, suppose we have the following form:

The Code is as follows:


<%
Response. Buffer = True
Response. ExpiresAbsolute = Now ()-1
Response. Expires = 0
Response. CacheControl = "no-cache"
If Len (Session ("FirstTimeToPage")> 0 then
& Single; the user has accessed the current page, and now returns access again.
& Single; clear session variables and redirect users to the logon page.
Session ("FirstTimeToPage") = ""
Response. Redirect "/Bar. asp"
Response. End
End If
& Single; if the program runs here, the user can view the current page
& Single; create a form from below
%>


We use the session variable FirstTimeToPage to check whether the user has accessed the current page for the first time. If it is not the first time (that is, the Session
("FirstTimeToPage") contains a value.) then, we clear the value of the session variable and redirect the user to a start page. In this way, when the form
At the time of submission (SompePage. asp is enabled at this time), we must assign a value to FirstTimeToPage. That is, in SomePage. asp, we need to add the following
Code:
Session ("FirstTimeToPage") = "NO"
In this way, if a user who has already opened SomePage. asp clicks the back button, the browser will re-request the server to download the page, and the server will check the Session
("FirstTimeToPage") contains a value, so the Session ("FirstTimeToPage") is cleared and the user is redirected to another page. Of course, all
All of this requires the user to enable the Cookie, otherwise the session variable will be invalid. (For more information about this problem, see For session variables.
To work, must the Web visitor have cookies enabled ?)
In addition, we can also use the client code to make the browser no longer cache Web pages:

The Code is as follows:









If you use the above method to force the browser to stop caching Web pages, pay attention to the following points:
"Pragma: no-cache" prevents the browser from caching pages only when secure connections are used. For pages that are not protected by security, "Pragma: no-cache"
It is considered to be the same as "Expires:-1". In this case, the browser still caches the page, but marks the page as expired immediately.
In IE 4 or 5, the Cache-Control META HTTP-EQUIV tag is ignored and does not work.
We can add all the code in practical applications. However, this method is not recommended because it cannot be applied to all browsers. However
In an Intranet environment, the administrator can control which browser the user uses. I think someone will use this method.
Iii. Other Methods
Next we will discuss the method to move the button back itself as the center, rather than the browser cache. Here is an article Rewiring the Back Button.
It is worth your reference. However, if this method is used, even if you click the back button, the user will not see the previous data input page
This is not the expected result because stubborn users can always find a way to bypass preventive measures.
Another way to disable the back button is to open a window without a toolbar with client JavaScript, which makes it difficult for users to return to the previous page,
Not impossible. A safer but rather annoying method is to open a new window when the form is submitted, while closing the window where the form is located. But I think
This method is not worth considering, because we cannot open a new window every time a user submits a form.
So, can we add JavaScript code to the page we don't want the user to return? The JavaScript code added to this page can be
It is used to produce the effect of clicking the forward button, which offsets the action generated by the user clicking the back button. The JavaScript code used to implement this function is as follows:
As shown in:

The Code is as follows:


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: info-contact@alibabacloud.com 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.