susanna
susanna

Reputation: 1521

html and href click only once

I have a link

<a href="new link">click me</a>

when a user click the link "click me", the new link page will be redirected and do something at the server side. The problem, if a user click the back button on the browser, it will go back to the old link. If the user click the link "click me", the new link page will be opened and processing at the server side will do again. How to prevent this situation? That is even a user go back to the old page, the new link cannot be clicked anymore

Upvotes: 2

Views: 3268

Answers (3)

RonaldBarzell
RonaldBarzell

Reputation: 3830

You can do this with Javascript. Here's example code that shows one way to do it:

<html>
<body>
<a href="#" onclick="f();">click me</a>
<script>
    var first = true;
    function f() {
        if ( first ) {
            first = false;
            window.open('http://www.google.com');
        }
    }
</script>
</body>
</html>

Alternatively, you could get rid of the global state variable and just disable or hide the link once it's clicked.

Upvotes: 0

Carlos Z
Carlos Z

Reputation: 13

You could try creating a new window and that would prevent end user from being able to go back however, you would need to create visited state to make the button disappear in the CSS.

<a href="new link" target="_blank">click me</a>

Other then that in pure HTML there is no way to stop a user from hitting the back button on a standard link.

Upvotes: 0

mellamokb
mellamokb

Reputation: 56769

Generate a unique id on the link, like therequest.do?id=1234, and log 1234 to a database so that the request cannot be run again.

  • User goes to mypage.do, server generates id 1234 and logs to database as unprocessed.
  • User clicks link to therequest.do?id=1234
  • Server does processing, and marks id 1234 as processed.
  • User clicks back button.
  • User clicks link again.
  • Server checks and sees request 1234 is already processed, so generates an error message on the screen: request has already been processed.

Upvotes: 2

Related Questions