Disable the backspace key and ASP. Net Press the delete key to return to the previous page.

Source: Internet
Author: User
Disable the backspace key and ASP. Net Press the delete key to return to the previous page.

During webpage creation, the backspace key can be rolled back to the previous page, and cache data is used to cause some errors. Some online articles:

Paste: Comparison of Methods for disabling the browser to move back --

 

 

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 ?" This is also one of the most frequently asked questions on the ASP forum. Unfortunately, the answer is very simple: we cannot disable the browser's back button.

At first, I felt incredible that someone wanted to disable the browser's back button. Later, I was relieved to see that there were so many people who wanted to disable this button (only the back button, not the browser's forward button ). By default, after submitting a form, you can use the back button to return 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 don't want 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 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 re-download the page, then the program can check the session variable to see whether the user should be allowed to open this 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

& 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), 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, we need to add the following code in somepage. asp:

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. (For more information about this issue, 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:

<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 historical record with the URL of the new page, so that there is only one page in the browsing history record, and the back button will never become

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.