Home Web Front-end JS Tutorial Clear web page history and block the back button!_javascript tips

Clear web page history and block the back button!_javascript tips

May 16, 2016 pm 06:57 PM
history record Web page

This article introduces various solutions for disabling the browser back button that can be found on the Internet, and analyzes their respective advantages, disadvantages and applicable occasions.
1. Overview
Many people have asked, "How can I 'disable' the browser's back button?", or "How can I prevent users from clicking the back button to return to previously visited pages?" "This question is also one of the most frequently asked questions on the ASP forum. Unfortunately, the answer is very simple: we can't disable the browser's back
button.
At first I was incredulous that someone would want to disable the browser’s back button. Later, I was relieved to see that so many people wanted to disable the back button
(the only ones they wanted to disable were the back button, not the browser's forward button). Because by default, after submitting the form, the user can return to the form page through the
back button (instead of using the "Edit" button!), and then edit and submit the form again to insert new records into the database. This is something we
don’t want to see.
So I decided to find a way to avoid this situation. I visited many websites and consulted various implementation methods introduced by these websites. If you
frequently visit ASP programming websites, you may have seen some of the content introduced in this article. The task of this article is to introduce all possible methods to everyone, and then find
the best method!
2. Disable caching
Among the many solutions I found, one of them suggested disabling page caching. Specifically, use a server-side script 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 revisit the server to download the page instead of reading the page from cache. When using this method, the programmer's main task is to create a session-level variable that determines whether the user can still view the page that is not suitable for access via the back button. Since the browser
no longer caches this page, the browser will re-download the page when the user clicks the back button, and the program can then check that session variable to see if
the user should be 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
&single; The user has visited the current page and is now returning to visit again.
&single; Clear session variables and redirect the user to the login page.
Session("FirstTimeToPage") = ""
Response.Redirect "/Bar.asp"
Response.End
End If
&single; If the program runs here, it means that the user can view Current page
&single; The following starts creating the form
%>


< /form>


We use the session variable FirstTimeToPage to check whether the user visits the current page for the first time. If it's not the first time (i.e. Session
("FirstTimeToPage") contains a certain value), then we clear the value of the session variable and redirect the user to a start page. In this way, when the form
is submitted (when SompePage.asp is opened), we must give FirstTimeToPage a value. That is, in SomePage.asp we need to add the following
code:
Session("FirstTimeToPage") = "NO"
In this way, if the user who has opened SomePage.asp clicks the back button, the browser The server will be re-requested to download the page. The server checks that Session
("FirstTimeToPage") contains a value, so it clears Session("FirstTimeToPage") and redirects the user to other pages. Of course,
all of this requires the user to have cookies enabled, otherwise the session variables will be invalid. (For more explanation of this problem, see For session variables
to work, must the Web visitor have cookies enabled?)
In addition, we can also use client-side code to prevent the browser from caching Web pages:



Copy code The code is as follows:








If you use the above method to force the browser to no longer cache web pages, you must pay attention to the following points:
"Pragma: no-cache" only prevents the browser from caching the page when using a secure connection. For pages that are not protected by security, "Pragma: no-cache"
is treated the same as "Expires: -1". At this time, the browser still caches the page, but marks the page as expiring immediately.
In IE 4 or 5, the "Cache-Control" META HTTP-EQUIV tag is ignored and has no effect.
  In practical applications we can add all these codes. However, since this method does not work in all browsers, it is not recommended. But
If it is an intranet environment and the administrator can control which browser the user uses, I think some people will still use this method.
3. Other methods
The method we are going to discuss next is centered on the back button itself, rather than the browser cache. Here is an article Rewiring the Back Button that is
worth referencing. However, I noticed that if this method is used, although the user will not see the page where he previously entered data when he clicks the back button, he only needs to click
twice. This is not the effect we want, because Many times, stubborn users are able to find ways to bypass preventive measures.
Another way to disable the back button is to use client-side JavaScript to open a window without a toolbar. This makes it difficult for the user 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 and at the same time close the window where the form is located. But I feel
that this method is not worthy of serious consideration, because we can’t let users open a new window every time they submit a form.
So, can we add JavaScript code to the page that we don’t want users to return to? The JavaScript code added to this page can
be used to produce the effect of clicking the forward button, thus counteracting the action caused by the user clicking the back button. The JavaScript code used to implement this function is as follows
:
Copy the code The code is as follows:

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to send web pages to desktop as shortcut in Edge browser? How to send web pages to desktop as shortcut in Edge browser? Mar 14, 2024 pm 05:22 PM

How to send web pages to the desktop as a shortcut in Edge browser? Many of our users want to display frequently used web pages on the desktop as shortcuts for the convenience of directly opening access pages, but they don’t know how to do it. In response to this problem, the editor of this issue will share the solution with the majority of users. , let’s take a look at the content shared in today’s software tutorial. The shortcut method of sending web pages to the desktop in Edge browser: 1. Open the software and click the "..." button on the page. 2. Select "Install this site as an application" in "Application" from the drop-down menu option. 3. Finally, click it in the pop-up window

Windows Security history is missing or not showing up in Windows 11 Windows Security history is missing or not showing up in Windows 11 Feb 28, 2024 pm 03:58 PM

Are you worried that your lost Windows Defender history hides infections? Windows Security's protection history tracks defenders' behavior to help you easily identify and remove threats. But what if your Windows security history is empty, missing, or not showing up? In this article, we'll look at the possible causes and the steps you can take to correct the problem. Why is Windows protection history missing? If you haven't cleared your Windows Defender protection history recently but it still appears blank, the reasons may be: History settings not enabled: If you disable history settings, Microsoft Defender is not configured correctly

What to do if the web page cannot be accessed What to do if the web page cannot be accessed Sep 06, 2023 am 09:36 AM

Solutions to inaccessible web pages include checking the network connection, clearing the browser cache, checking the web page address, trying to use other browsers, checking the server status, checking the domain name resolution, checking the firewall and security settings and contacting the website administrator. Detailed introduction: 1. Check the network connection to ensure that the network connection is normal. You can try to open other web pages or use other devices to access to determine whether it is a network connection problem. If other web pages can be accessed normally, it may be a problem with the web page; 2. Clear the browser cache. The browser cache may cause the web page to fail to load, etc.

How to set up web page automatic refresh How to set up web page automatic refresh Oct 26, 2023 am 10:52 AM

To set the automatic refresh of a web page, you can use the HTML "meta" tag, the JavaScript "setTimeout" function, the "setInterval" function or the HTTP "Refresh" header. Detailed introduction: 1. Use the "meta" tag of HTML. In the "<head>" tag of the HTML document, you can use the "meta" tag to set the automatic refresh of the web page; 2. The "setTimeout" function of JavaScript, etc.

What should I do if the images on the webpage cannot be loaded? 6 solutions What should I do if the images on the webpage cannot be loaded? 6 solutions Mar 15, 2024 am 10:30 AM

Some netizens found that when they opened the browser web page, the pictures on the web page could not be loaded for a long time. What happened? I checked that the network is normal, so where is the problem? The editor below will introduce to you six solutions to the problem that web page images cannot be loaded. Web page images cannot be loaded: 1. Internet speed problem The web page cannot display images. It may be because the computer's Internet speed is relatively slow and there are more softwares opened on the computer. And the images we access are relatively large, which may be due to loading timeout. As a result, the picture cannot be displayed. You can turn off the software that consumes more network speed. You can go to the task manager to check. 2. Too many visitors. If the webpage cannot display pictures, it may be because the webpages we visited were visited at the same time.

Possible reasons why the network connection is normal but the browser cannot access the web page Possible reasons why the network connection is normal but the browser cannot access the web page Feb 19, 2024 pm 03:45 PM

The browser cannot open the web page but the network is normal. There are many possible reasons. When this problem occurs, we need to investigate step by step to determine the specific cause and solve the problem. First, determine whether the webpage cannot be opened is limited to a specific browser or whether all browsers cannot open the webpage. If only one browser cannot open the web page, you can try to use other browsers, such as Google Chrome, Firefox, etc., for testing. If other browsers are able to open the page correctly, the problem is most likely with that specific browser, possibly

How to view viewing history on Kuaishou? Where is the viewing history on Kuaishou? How to view viewing history on Kuaishou? Where is the viewing history on Kuaishou? Mar 20, 2024 pm 06:50 PM

Kuaishou is a short video viewing platform used by many users. It is a good choice for passing time. It can relieve worries and boredom. Everyone can watch some short videos every day. Of course, sometimes everyone , I accidentally swiped my hand too fast and accidentally swiped over the video I liked. As a result, no one has collected these good-looking videos, so everyone still wants to watch these videos again, so you can use Use the viewing history on the software to see all the videos you have watched before, so that you can better find videos. You can view them in a few simple steps. Come and try it out. How to view Kuaishou's browsing history: 1. First enter Kuaishou, and then click on the three horizontal lines in the upper left corner. 2. Then click

What to do if the webpage cannot be opened What to do if the webpage cannot be opened Feb 21, 2024 am 10:24 AM

How to solve the problem of web pages not opening With the rapid development of the Internet, people increasingly rely on the Internet to obtain information, communicate and entertain. However, sometimes we encounter the problem that the web page cannot be opened, which brings us a lot of trouble. This article will introduce you to some common methods to help solve the problem of web pages not opening. First, we need to determine why the web page cannot be opened. Possible reasons include network problems, server problems, browser settings problems, etc. Here are some solutions: Check network connection: First, we need

See all articles