Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What if I use #! instead of # in href of anchor tag <a>?

Tags:

html

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>
like image 543
shahid mohammad Avatar asked Dec 30 '16 10:12

shahid mohammad


People also ask

How do you use what if?

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?

What are the IF conditionals?

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.

What does if mean in a sentence?

noun. informal. a hypothetical question; speculation. one of the great what-ifs of modern history. Collins English Dictionary.


1 Answers

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?

  • It is not semantically correct. Links should be links.
  • It is a hack: You are linking to the element with id="!" and depending on the error recovery that happens when that element doesn't exist.
  • It is allowed under the rules of what constitutes valid HTML.
  • It is probably OK according to the various bits of accessibility legislation about the work which don't generally care if something works without JavaScript

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:

  • Everyone has JavaScript, right?
  • Progressive Enhancement
  • Unobtrusive JavaScript
like image 178
Quentin Avatar answered Oct 01 '22 16:10

Quentin