Clear the web page history and unmask the button!

Source: Internet
Author: User

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:
Copy codeThe 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:
Copy codeThe 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
%>
<Form method = post action = "SomePage. asp">
<Input type = submit>
</Form>

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:
Copy codeThe Code is as follows:
<Html>
<Head>
<Meta http-equiv = "Expires" CONTENT = "0">
<Meta http-equiv = "Cache-Control" CONTENT = "no-cache">
<Meta http-equiv = "Pragma" CONTENT = "no-cache">
</Head>

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:
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Javascript: window. history. forward (1 );
// -->
</Script>

Similarly, although this method is effective, it is still far from the "best method. Later, I saw someone suggested using location. replace from
Go to another page. The principle of this method is to replace the current historical record with the URL of the new page, so that there is only one page in the browsing history record.
The return 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. This
The method example is as follows:
<A href = "PageName.htm" onclick = "javascript: location. replace (this. href );
Event. returnValue = false; ">
Do not go back to the link on this page </A>
Try the following link:
Do not go back to the link on this page!
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
.
Click the link above to open a simple HTML page. Click the back button. You can see that this page is not opened, but the current page.
Previous Page! (Of course, you must enable the client JavaScript code in the browser .)
After careful searching, I found that I still cannot find a way to completely disable the browser's back button. All methods described here
Users can be prohibited from returning to the previous page in different ways to varying degrees, but they all have their own limitations. Because the button does not exist, you can disable the button.
So the best solution is to mix client scripts and server scripts.

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: 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.