You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Lint rules don't allow links with javascript:void href, or any alternative. #4141
For a variety of valid reasons, you may want to have html anchors with a href that is null for a time. One way of doing this is href='javascript:void(0)', however this triggers: Script URL is a form of eval no-script-url" lint error.
Another way is href='#'. This triggers: Links must not point to "#". Use a more descriptive href or use a button instead jsx-a11y/href-no-hash
Another way is <a href>, This triggers: Warning: Received 'true' for a non-boolean attribute 'href'.
Are there ways to achieve the desired effect without lint errors, otherwise can one of these be removed?
I'm experiencing pretty much the same as you, I get this error from my navbar links:
Use a more descriptive href or use a button instead jsx-a11y/href-no-hash
Confuses the hell outta me, but the page does display.
For a variety of valid reasons, you may want to have html anchors with a href that is null for a time
Could you please tell me these reasons? I recall using this technique in early 2000s when I was not familiar even with jQuery. Not sure if it is viable now.
I use href='# ' (notice the space after #) which retains the css of a tag and doesn't alter the URL in any other way a normal # would do.
Could you please tell me these reasons? I recall using this technique in early 2000s when I was not familiar even with jQuery. Not sure if it is viable now.
Just for visually design purposes when the route to that specific is either not defined or not implemented. I'd rather use the href attribute than add a class to use cursor: pointer
Reacted by John Datserakis, filipe.morais, Decentraliser🌵, Tony Davidson, Carol and jp06
Since there is no other way to make an anchor tag clickable I'd be curious why anyone would want this rule to exist, let alone be the default, and not have a way to override it.
Reacted by Dachi Natsvlishvili, ghayes7, Verner Läll, Erik Sytnyk, Danny Freed, Benjamin Bourasseau, darzyx, Dmitry Astrikov, Valentina López, The Tran and 3 more
Would be really nice to remove that rule. Currently, in react-scripts 1.x it is not enforced, so having a big codebase it is hard to try out 2.0 version, without significate changes in your code.
Now the button can be used inline in text as one can with anchors.
I also inverted how the underline works from that of Bootstrap to display as links by default with the underline removed on hover. However the other option can just as easily be done.
But to expand on it, abusing an anchor tag for anything but navigation goes way beyond being contrary to the specification of this element. It causes a very real detrimental experience to many users out there when confronted with this situation.
Here I am referring to any user that uses a keyboard to navigate the internet. That includes users who rely on screen reader software to use the web. Exact statistics are hard to find due to the privacy impact it would have, but one study shows that the number of users likely to use a keyboard alone just due to a disability being about 7% of the working adults in the UK, US and Canada. https://www.powermapper.com/blog/website-accessibility-disability-statistics/
This does not even include those who choose to use a keyboard or sustained a temporary disability like a broken arm etc.
The point is we are talking about millions and millions of people.
If an anchor has no href it does not act a link. It becomes, what the spec calls, a placeholder. In fact you are unable to select and activate it using a keyboard alone. Regardless of whether it contains an onClick handler or not. If this element is important to the function of the application it means that the application would block around 7% or more of the population from using this application at all.
If the anchor container a dummy href you will be able to interact with it using the keyboard, however the interaction will be unexpected. Screen reader users have the elements announced to them, so, if you come across a link it says "Link", regardless of how it is styled. And links are expected to navigate. It is extremely confusing to a screen reader user when they encounter a link, activate it and no navigation happens.
By using the fix @gaearon suggests above and turning it into a button, EVERYONE can interact with it. All keyboard users can interact with it while screen reader users hear "Button" and expect it to perform an action and not a navigation. All that, without having to hack the href or suppress the rule.
The rule is not there to be an irritation but to try and assist us in making applications that can be used by the widest group of users possible :-)
Reacted by Satyajit Sahoo, dan, Job van Achterberg, Joshua, Muhammad Rosyid and Doug Smith
I'm aware of the rationale. In fact the desire to keep semantic navigation elements as anchors rather than use a button was the entire reason I didn't just add an onClick to a different element.
Semantic navigation elements are very important indeed. But the reason I thought to add some information is that the browser implementation of the <a> element differs when it has an href to when it doesn't.
Without an href the element does not accept keyboard focus. So a user using, for example, the NVDA screen reader would completely miss the element if navigating a page in focus mode. The user would need to know to switch to browse mode in the screen reader and go and hunt for the element to interact with it. And whether or not the user would even be able to interact with it would depend on which screen reader and which browser they use.
For sighted keyboard-only users there would be absolutely no way to interact with the element, even if it has an onClick handler as you cannot Tab to it.
So the discussion is not about the semantics of the element itself, but if people can even use it at all. By giving it an onClick handler and a null href value and then styling it the same as all other <a> elements creates situations that will completely block a large number of users.
So the suggestion would be:
If the element needs no onClick handler and has no href, rather use a span.
If it needs an onClick handler and has no href, rather use a button and style it as a link if that is required. It is perfectly best practice to do a navigation as the result of an action performed by a button.
If the element is in fact a navigation element then it can simply get a good href.
To clarify, the situation that I initially had, which differs from other peoples cases here, was an anchor that had a null href for a time, but never had an onClick. It was a pure navigational element that needed a null href while some async logic happened.
But this was months ago, I found a workaround, I no longer really care about this ticket. Strong lint rules are annoying when you have an edge-case, such as mine.
You can eslint disable, you can use one of the many hacks in this thread, you can give up and use a span, or you can eject and change the lint rules. Many options, each requiring cognitive load, for what ultimately is unimportant.
Life is short, I'm assuming this is WONTFIX. Closing the issue to save my inbox.
Activity
Timer commentedon Mar 12, 2018
href={null}should work.peterbraden commentedon Mar 13, 2018
This doesn't work as it means that the href isn't passed to the dom, meaning that the style of the anchor doesn't get the link attributes.
CloudMunk commentedon Mar 22, 2018
I'm experiencing pretty much the same as you, I get this error from my navbar links:
Use a more descriptive href or use a button instead jsx-a11y/href-no-hash
Confuses the hell outta me, but the page does display.
Timer commentedon Mar 23, 2018
My apologies, maybe we should consider loosening this rule...
miraage commentedon Apr 6, 2018
Could you please tell me these reasons? I recall using this technique in early 2000s when I was not familiar even with jQuery. Not sure if it is viable now.
ejose19 commentedon Apr 28, 2018
I use href='# ' (notice the space after #) which retains the css of a tag and doesn't alter the URL in any other way a normal # would do.
Just for visually design purposes when the route to that specific is either not defined or not implemented. I'd rather use the href attribute than add a class to use cursor: pointer
dsteinman commentedon Jun 28, 2018
Since there is no other way to make an anchor tag clickable I'd be curious why anyone would want this rule to exist, let alone be the default, and not have a way to override it.
abegehr commentedon Jul 8, 2018
bump 👍
please get rid of this rule
feat: Make neighbourhood links clickable
erik-sytnyk commentedon Sep 3, 2018
Would be really nice to remove that rule. Currently, in react-scripts 1.x it is not enforced, so having a big codebase it is hard to try out 2.0 version, without significate changes in your code.
4 remaining items
gaearon commentedon Oct 1, 2018
I think instead of disabling this line we need to properly explain how to style buttons like links.
Just like Bootstrap does.
For example:
Then use a button:
AFAIK that would correctly behave in an accessible way without hacks.
AlmeroSteyn commentedon Oct 2, 2018
Because
anchorsareinlineelements the style should probably also cater for this use case as well.So I suggest:
Now the button can be used inline in text as one can with
anchors.I also inverted how the underline works from that of Bootstrap to display as links by default with the underline removed on hover. However the other option can just as easily be done.
alfonsomartinde commentedon Oct 2, 2018
To get what @peterbraden asked, I just write "#/" as href value:
<a href="#/">...</a>The warning is gone, and the output HTML is valid.
satya164 commentedon Oct 3, 2018
It's still incorrect use of an anchor tag. Why not use button?
AlmeroSteyn commentedon Oct 3, 2018
Maybe some clarification is in order as to why this rule exists and why it is a good idea to follow it instead of finding ways to go around it.
I currently have a PR open that will make the error message clearer and also show where to find more information. The current docs for that can be found here https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-is-valid.md. It contains an explanation of why this rule exists.
But to expand on it, abusing an
anchortag for anything but navigation goes way beyond being contrary to the specification of this element. It causes a very real detrimental experience to many users out there when confronted with this situation.Here I am referring to any user that uses a keyboard to navigate the internet. That includes users who rely on screen reader software to use the web. Exact statistics are hard to find due to the privacy impact it would have, but one study shows that the number of users likely to use a keyboard alone just due to a disability being about 7% of the working adults in the UK, US and Canada. https://www.powermapper.com/blog/website-accessibility-disability-statistics/
This does not even include those who choose to use a keyboard or sustained a temporary disability like a broken arm etc.
The point is we are talking about millions and millions of people.
If an
anchorhas nohrefit does not act a link. It becomes, what the spec calls, a placeholder. In fact you are unable to select and activate it using a keyboard alone. Regardless of whether it contains an onClick handler or not. If this element is important to the function of the application it means that the application would block around 7% or more of the population from using this application at all.If the
anchorcontainer a dummyhrefyou will be able to interact with it using the keyboard, however the interaction will be unexpected. Screen reader users have the elements announced to them, so, if you come across a link it says "Link", regardless of how it is styled. And links are expected to navigate. It is extremely confusing to a screen reader user when they encounter a link, activate it and no navigation happens.By using the fix @gaearon suggests above and turning it into a button, EVERYONE can interact with it. All keyboard users can interact with it while screen reader users hear "Button" and expect it to perform an action and not a navigation. All that, without having to hack the
hrefor suppress the rule.The rule is not there to be an irritation but to try and assist us in making applications that can be used by the widest group of users possible :-)
peterbraden commentedon Oct 4, 2018
I'm aware of the rationale. In fact the desire to keep semantic navigation elements as anchors rather than use a button was the entire reason I didn't just add an onClick to a different element.
AlmeroSteyn commentedon Oct 5, 2018
Thank you for responding.
Semantic navigation elements are very important indeed. But the reason I thought to add some information is that the browser implementation of the
<a>element differs when it has anhrefto when it doesn't.Without an
hrefthe element does not accept keyboard focus. So a user using, for example, the NVDA screen reader would completely miss the element if navigating a page in focus mode. The user would need to know to switch to browse mode in the screen reader and go and hunt for the element to interact with it. And whether or not the user would even be able to interact with it would depend on which screen reader and which browser they use.For sighted keyboard-only users there would be absolutely no way to interact with the element, even if it has an
onClickhandler as you cannotTabto it.So the discussion is not about the semantics of the element itself, but if people can even use it at all. By giving it an
onClickhandler and a nullhrefvalue and then styling it the same as all other<a>elements creates situations that will completely block a large number of users.So the suggestion would be:
onClickhandler and has nohref, rather use aspan.onClickhandler and has nohref, rather use a button and style it as a link if that is required. It is perfectly best practice to do a navigation as the result of an action performed by a button.href.peterbraden commentedon Oct 5, 2018
To clarify, the situation that I initially had, which differs from other peoples cases here, was an anchor that had a null href for a time, but never had an onClick. It was a pure navigational element that needed a null href while some async logic happened.
But this was months ago, I found a workaround, I no longer really care about this ticket. Strong lint rules are annoying when you have an edge-case, such as mine.
gaearon commentedon Oct 5, 2018
You can always
// eslint-disable-next-linefor an edge case.peterbraden commentedon Oct 5, 2018
You can eslint disable, you can use one of the many hacks in this thread, you can give up and use a span, or you can eject and change the lint rules. Many options, each requiring cognitive load, for what ultimately is unimportant.
Life is short, I'm assuming this is WONTFIX. Closing the issue to save my inbox.
gaearon commentedon Oct 5, 2018
You can also unsubscribe :-) I'd feel better about closing after https://github.com/evcohen/eslint-plugin-jsx-a11y/pull/486 gets out and we update our lint message to be more specific and useful. But either is fine.