多種屏蔽瀏覽器的後退按鈕的方法

來源:互聯網
上載者:User
按鈕|瀏覽器 I have had a lot of people ask, "How to I 慸isable?the back button?" or, "How do I prevent a user from
clicking the back button and going back to the previous screen?" In fact, this is one of the most commonly
asked questions on the ASPMessageboard and, sadly, the answer is quite simple: You CANNOT disable the back
button of the browser.

Initially I could not figure why anyone would want or need to do that. Then it struck me as to why so many
people would want to disable the back button. (Not the forward button mind you only the back button.) When
a user submits an application and then goes back "using the back button" to make a change instead of
clicking on "Edit," a new record will be inserted ?we don抰 want that, now do we? Again if the user
finished a page and then went back to that page and continued to make changes and saved them we would not
want that either.

So I decided to figure a way or ways to prevent this scenario. I started doing a bit of research all over
the Net going into various sites so basically this article will have a lot of stuff you might have already
read if you looked on the Net. I am just trying to put it all in one place and find the "best" way of
doing it!

One of the many suggestions I got was to prevent the page from being cached. This can be done with server-
side script:

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



This method works great! It forces the browser to go to the server to get the page instead of from its
cache. What you will want to do is create a Session-level variable that determines whether or not a user
can still "view" the page that you do not want to let the user navigate back to. Since the page is not
being cached on the browser, the page will be reloaded when the user hits the back button, and you can
check for that session-level variable to see if the user can view this page or not.

For example, we could create a form like so:

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

If Len(Session("FirstTimeToPage")) > 0 then
'The user has come back to this page after having visited
'it... wipe out the session variable and redirect them back
'to the login page
Session("FirstTimeToPage") = ""
Response.Redirect "/Bar.asp"
Response.End
End If

'If we reach here, the user can view the page, create the form
%>

<form method=post action="SomePage.asp">
<input type=submit>
</form>



Note that we are using a Session variable (FirstTimeToPage) to check to see if this is the users first
visit to this particular page. If it isn't (that is, if Session("FirstTimeToPage") contains any value),
then we clear out the session variable and redirect the user back to some starting page. Now, when the
form is submitted (and SomePage.asp is loaded), we must set the session variable FirstTimeToPage to some
value. So... in SomePage.asp we'd need code like:

Session("FirstTimeToPage") = "NO"


Then, if the user, on SomePage.asp, hits the back button, the browser will requery the Web server, see
that Session("FirstTimeToPage") contains some value, clear Session("FirstTimeToPage"), and redirect the
user to some page. All of this hinges, of course, on the fact that the user has cookies enabled, else
session variables won't work! (For more information on this subject, be sure to check out the FAQ: For
session variables to work, must the Web visitor have cookies enabled?)

You can also use client-side code to force the user's browser to not cache a Web page.

<html>
<head>
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
</head>



There are a couple things to keep in mind when using the above method to force a browser to not cache a
Web page:


Pragma: no-cache prevents caching only when used over a secure connection. A Pragma: no-cache META tag is
treated iden



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。