0

I have a link that needs to be disabled once I click on it until the page reloads again. The main purpose is to limit the user from clicking on the link multiple times.

Below is my link:

<a href="/User/faces/profile.xhtml?id=20521&amp;itemNo=-1" onclick="this.disabled=true;document.body.style.cursor='wait';" style="padding: 10px 5px;">Profile</a>

This works fine on ie11, but not on chrome or iPad.

Is there anything that I am missing? Any help or hint is appreciated.

6
  • How do you know it is not disabled if it redirects you to other page Commented May 21, 2019 at 18:39
  • The JSF pre-render methods in the new page are getting executed multiple times based on the number of clicks until the page reloads. Commented May 21, 2019 at 18:42
  • try checking console. is it clear? Commented May 21, 2019 at 18:48
  • You'll have to preventDefault() and, modify the element, and then redirect to the profile.xhml page programmatically. Commented May 21, 2019 at 18:51
  • Yes, it is clear. I don't see any errors on chrome console. Commented May 21, 2019 at 18:51

2 Answers 2

2

You can't disable an anchor element by using disabled. There are some ways to achieve it of course. One of them would be to set pointer-events: none per css (but you would lose the wait-cursor style with it). There are others like removing the href attribute or changing it, but this could have sideeffects depending on your environment as well.

My suggestion would be to just keep a boolean state and prevent execution if the link was already clicked:

const link = document.querySelector('#myLink');
let redirected = false;
link.addEventListener('click', ev => {
    if (redirected) {
        ev.preventDefault();
    } else {
        console.log('click');
        redirected = true;
        link.style.cursor = 'wait';
    }
});
<a href="http://www.google.de" target="_blank" id="myLink">My Link</a>

Sign up to request clarification or add additional context in comments.

1 Comment

It worked on my local server, but had problems when deployed on Solaris. It was getting submitted multiple times. So, eventually I added a hidden button and click it using JavaScript onClick event. Now, disable the hidden button onClick of it. Not an elegant solution, but it resolved the issue.
1

The disabled property of an element //- they are now calling attribute - in other plain browsers related to Netscape Navigator doesn't have support the property disabled on other than a narrow group of page controls they also mistake for elements...

The most recent table of support are the following tags:

<button> <fieldset> <input> <optgroup> <option> <select> <textarea>

Meaning : There is no support for Link \ Anchor elements, except in versions of IE 1997 and up.

For a wider support I suggest you try using a button tag for your code.

1 Comment

Added a hidden button that is clicked onClick of the link. Once the button is clicked I am disabling it from further clicks. It solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.