How to invalidate IE's backend button

Source: Internet
Author: User

NextArticleThis is excerpted from a newsgroup. The source is unknown. See the following section.:

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 Internet, and analyzes their advantages and disadvantages and applicable scenarios.

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 previously browsed pages ?" I visited many websites and referred to various implementation methods described by these websites. If you frequently access 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 and find the best method!

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:
<%
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 main task is to create a session-level variable, using this variable to determine whether the user can still view the page that is not suitable for access through the back button. Because the browser no longer caches this page, when the user clicks the back button, the browser will download the page again. Program You can check the session variable to see if the user is allowed to open the page.
For example, suppose we have the following form:
<%
Response. Buffer = true
Response. expiresabsolute = now ()-1
Response. expires = 0
Response. cachecontrol = "no-Cache"
If Len (Session ("firsttimetopage")> 0 then
"The user has accessed the current page and now returns access again.
"Clear session variables and redirect users to the logon page.
Session ("firsttimetopage") = ""
Response. Redirect "/bar. asp"
Response. End
End if
"If the program runs here, you can view the current page.
"Create a form as follows
%>
<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), we will clear the value of the session variable and redirect the user to a start page. In this way, when the form is submitted (sompepage. asp is opened at this time), we must assign firsttimetopage a value. That is, in somepage. asp, we need to add the following Code :
Session ("firsttimetopage") = "no"
In this way, the somepage has been enabled. if ASP users click the back button, the browser will re-request the server to download the page. The server checks that session ("firsttimetopage") contains a value, so the session ("firsttimetopage") is cleared "), and redirect the user to other pages. Of course, all of this requires the user to enable the cookie, otherwise the session variable will be invalid.
In addition, we can also use the client code to make the browser no longer cache web pages:
<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 unprotected pages, "Pragma: No-Cache" 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 for reference. However, I have noticed that if this method is used, even though the user does not see the previous data input page when clicking the back button, it only needs to be clicked twice, which is not the expected effect, many times, 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, but 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. However, 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 used to produce the effect of clicking the forward button, which offsets the action generated by clicking the back button. The javascript code used to implement this function 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 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 example using this method 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
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 access records.
Click the link above to open a simple HTML page. Click the back button to open the page instead of the 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 the methods described here can prohibit users from returning to the previous page in different ways to varying degrees, but they all have their own limitations. Because there is no way to completely disable the back button, the best solution should be to mix client scripts and server scripts.

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.