Home

Disabling The Back Button - Sort Of

TLDR - Bit of JavaScript. It works but it's ugly.

    <script>
        function NoBack() {
            if (sessionStorage["h1"] == undefined) {
                sessionStorage.setItem("h1", "x")
            }
            if (sessionStorage["h2"] == undefined) {
                sessionStorage.setItem("h2", "x")
            }
            if (sessionStorage["h3"] == undefined) {
                sessionStorage.setItem("h3", "x")
            }

            sessionStorage.setItem("h3", sessionStorage.getItem("h2"))
            sessionStorage.setItem("h2", sessionStorage.getItem("h1"))
            sessionStorage.setItem("h1", document.location)

            if (sessionStorage.getItem("h1") == sessionStorage.getItem("h3")) {
                sessionStorage.setItem("h2", "x")
                history.forward()
            }
            
        }
    </script>

<body onload = "NoBack()">
............

Why oh why oh why can you not stop users using the back button in browsers? It don't 'arf mess up our lovely beautiful code. 

Let us create a scenario. The user needs to enter the details of the widget that has come into the factory. We present a page with several boxes, dropdowns and radio buttons. The user fills in the details and clicks "Submit". 

On doing so our code dutifully "INSERT"s" INTO" the database the details of the new widget. Then the user remembers that they entered "brown" in the colour choice and not "blue". Not to worry, hit the back button, change the option to "blue" and Submit.

At which point our code dutifully "INSERT"s" INTO" **ANOTHER** widget rather than "UPDATE"ing the previous entry. We now have 2 widgets, one blue, one brown, rather than the one blue widget we should have. 

The correct way to deal with this is in your code. 

You could ensure every widget coming in has a unique code. The user enters the code and you can use REPLACE INTO to either enter new or overwrite if the unique code already exists.

You could create a unique page identifier to see if THIS page has been used before. A hidden textbox with an accurate timestamp, maybe the timestamp is saved with the widget thus creating the unique code above or saved in the session to check if it's been used or not?

The point is stopping the user using the back button is not the BEST solution. But what is right and proper and good is not that which we are faced with in the real world. There are times when a coder just has to do the best they can and face the consequences.

You... can't stop the back button being pressed. There's no "off" switch or magical JavaScript to make it stop working. All you can do is bodge the system such that if they go back then they're sent forward - again. 

The code above does this. It makes notes of where the user has been and then if it sees a match it uses JavaScript to push the forwards once more.

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog