开发者

Is it good practice (or a good idea) to leave out HREF on links that are hooked up via JavaScript?

Expanding on this question,

For items that trigger dialogs and menus (i.e. non navigational), is 开发者_JAVA百科it good practice to leave out the HREF attribute in links that have events that are hooked up via JavaScript? In these cases, does it makes sense to have HREF there at all?

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

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

to this: <a>some text</a>


==Edited a little more==

Bad, bad idea. It wont show up as a link for one thing. If you need a button, but are use an <a> as one, just using a <button> or <input type="button">. As you said, "non-navigational". The entire point of <a> is navigational.

Out of those two tho, use href="#" putting javascript:// in a link is worse than adding inline styles.


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

  1. have the href, if not you need to set the cursor to hand or pointer
  2. make the href go to a "sorry you need javascript" page if you do not want to use # or as I learned recently #somethingNotExisting
  3. NEVER have href="javascript:anything()"
  4. return false or preventDefault in the onclick which is preferably set in an onload handler

UPDATE: For menus and such, the agreed markup are lists with css and using links in such menus is recommended if the links actually loads content to gracefully degrade to plain html navigation if script is off


You should use the command element instead.

The command element represents a command that the user can invoke.

HTML5: Edition for Web Authors

This has the benefit of being semantically correct. There's at least one fork of html5shiv which 'enables' support for the element in older browsers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜