How to prevent users form double clicking

ces123

Honorable
Dec 28, 2012
1
0
10,510
My clients are used to double clicking even if they are not supposed to. I want to tweak the java script to dynamically disable links/buttons after a click. But how? i don't know an iota about java script.
 

randomizer

Distinguished
It's amazing what years of clicking desktop icons will do to people :)

This should work in most browsers, although I haven't actually tested it in much. It will "disable" all links, buttons and inputs that are of the button or submit type (mostly for forms) when clicked. It shouldn't interfere with anything else that happens when you click the buttons on your site, but if your links do anything other than navigate to another page this will break the extra functionality.

Note that it also requires that you have the JavaScript library jQuery available. You can also achieve similar functionality without external libraries with a bit more effort (I actually had some code here earlier, but it wasn't very good so I removed it).

JavaScript:
(function ($) {
    $(document).on('click', 'button,input[type="button"],input[type="submit"]', function () {
        this.disabled = true;
    });

    $(document).on('click', 'a', function (e) {
        window.location.href = this.href;
        this.href = '#';
    });
}(jQuery));

This can go in a script tag at the bottom of the page (right before the body) or, preferably, in a separate JavaScript file that you can include with a script tag inside the document's head tag. You may already have one you can put it in.

Are you able to involve the developer who built the website in the first place, or someone else with web development skills? They're in a better position to make these sorts of changes because just copying and pasting code when you don't know an iota of JavaScript is potentially quite dangerous. After all, you may not want to disable all links and buttons.