Good practice (or good idea) to leave HREF in links that are connected via JavaScript?

Turning to this question ,

For elements that trigger dialogs and menus (i.e. not navigation), is it useful to leave the HREF attribute in the links that are associated with JavaScript related events? In these cases, does it make sense to have HREF there at all?

From this: <a href="javascript://">some text</a>

Or, even worse, this is: <a href="#">some text</a> (which forces you to use event.preventDefault() )

: <a>some text</a>

+4
source share
3 answers

== Edited a bit more ==

Bad, bad idea. It will not appear as a link for one thing. If you need a button, but use <a> as one, just using <button> or <input type="button"> . As you said, "out of shipping." The whole point <a> is navigational.

Of these two methods, use href="#" , putting javascript:// in the link is worse than adding inline styles.

+3
source

Pragmatically - this is what I learned over 16 years of JS

  • there is href, if not, you need to set the cursor to the side or the pointer
  • make href go to the page "sorry, you need javascript" if you do not want to use # or as I recently found out #somethingNotExisting
  • NEVER has href = "javascript: anything ()"
  • return false or preventDefault in onclick, which is preferably set in the onload handler

UPDATE: for menu, etc. consistent markup is lists with css, and links in such menus are recommended if links really load content in order to gracefully degrade to simple html navigation if the script is turned off

+1
source

Instead, you should use the command element.

A command element is a command that a user can invoke.

HTML5: Web Authors Edition

It makes sense to be semantically correct. There is at least one html5shiv fork that "allows" to support the element in older browsers.

0
source

All Articles