I have used #! in href of a tag in my website. It does not throws the user on top of the page if he/she clicks the link. But my question is, is it legal?
<a href="#!">Click here</a>
instead of
<a href="#">Click here</a>
We use what if at the beginning of a question when we are asking about the consequences of an action, particularly one that is undesirable. We refer in this way to present or future circumstances: What if I am made redundant and have no work? What shall we do then?
A conditional sentence is based on the word 'if'. There are always two parts to a conditional sentence – one part beginning with 'if' to describe a possible situation, and the second part which describes the consequence. For example: If it rains, we'll get wet.
noun. informal. a hypothetical question; speculation. one of the great what-ifs of modern history. Collins English Dictionary.
It seems your goal is to create something that the user can click on in order to trigger some JavaScript and you don't want side effects (like navigating to the top of the page).
is it legal?
id="!"
and depending on the error recovery that happens when that element doesn't exist.The correct way to present a clickable control which exists only to bind JavaScript to is to use a button.
<button type="button">Click here</button>
You can apply CSS to make it look however you like though, so don't let "But it I want it to look like a link" stop you.
A more robust approach would be to use a server side fallback (i.e. a form submission or a link) and to cancel the default behaviour of the control by calling the preventDefault
method of the event object.
Further reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With