最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - What is the right href value for a JavaScript anchor tag? - Stack Overflow

programmeradmin3浏览0评论

I usually have to bind a JavaScript function to an anchor-click event. That is easy using jquery or the onclick inline attribute.

But, my problem is that I never know what the best way to keep href empty is.

For instance:

  1. <a href="javascript:void(0)"> - It seems like a bit too much code for just being empty
  2. <a href=#> - If I don't want to move to another page, I must return false in the JavaScript call
  3. <a href> - This option breaks the cursor and hover style and the browser doesn't render it as a link
  4. <a> - idem

What is the best href value for empty anchors? I'm not interested to keep functionality without JavaScript

I usually have to bind a JavaScript function to an anchor-click event. That is easy using jquery or the onclick inline attribute.

But, my problem is that I never know what the best way to keep href empty is.

For instance:

  1. <a href="javascript:void(0)"> - It seems like a bit too much code for just being empty
  2. <a href=#> - If I don't want to move to another page, I must return false in the JavaScript call
  3. <a href> - This option breaks the cursor and hover style and the browser doesn't render it as a link
  4. <a> - idem

What is the best href value for empty anchors? I'm not interested to keep functionality without JavaScript

Share edited Dec 14, 2017 at 3:53 Andrew Li 58k14 gold badges134 silver badges148 bronze badges asked Feb 16, 2012 at 14:06 Martin BorthiryMartin Borthiry 5,32610 gold badges43 silver badges59 bronze badges 3
  • possible duplicate of Href for JavaScript links: "#" or "javascript:void(0)"? – JJJ Commented Feb 16, 2012 at 14:09
  • See also: stackoverflow./questions/4842953/or-javascriptvoid0 – Felix Kling Commented Feb 16, 2012 at 14:18
  • 1 If the element does not have the function of a link, then don't use a link. Use a button instead. – Felix Kling Commented Feb 16, 2012 at 14:35
Add a ment  | 

6 Answers 6

Reset to default 3

The right one is to use an empty a element href attribute and bind the click event in Javascript.

For unobtrusive design, you should have a href attribute with a proper link (so those without Javascript can still use the site) and remove the attribute in Javascript, binding the click event.

If you are simply using the a element as a target to bind the click event to, consider using a div or span instead.

I'm personally a firm believe in using JavaScript to extend functionality, not replace. With that said, I leave anchors pointing to a "safe" fall-back of the action I'm really just executing with javascript. Simply put:

<a href="/users/create" class="user-create"></a>

Then, supplement (and return false) if javascript was able to successfully load and bind to the element, otherwise still provide the user the ability to acplish the task if they don't have javascript (either blocked via plugin or just not loaded).

Simply do not use A element. You can as well make DIV clickable or any other element.

Or you can also simply leave href attribute out, like so.

<a onclick="myFunction();">dasd</a>

If you also want to look it like a link, put this in CSS:

a {
  text-decoration: underline;
  color: blue;    
}​

I think

<a href="javascript:;"></a>

or

<a href="javascript:void;"></a>

is the best way to indicate empty anchor.

and

<a href="#someId"></a>

will move the page to the dom element which has id="someId"

it all depends. if you are clicking an anchor to open a panel on the page then I am happy to use option 2 but only if I insert that anchor with javascript.

this then means with javascript disabled the anchor doesn't show and the panel should be visible.

if the link goes somewhere then you need the actual link address like brad christy and odid said.

<a href=#>ABC</a>

=> on click of above link it will set url in address bar which makes flickering of document or resetting scroll position

to avoid above problem, <a href=# onclick="return false" >abc</a> can be used and bind event handler using jQuery as usual or you can execute any function on onclick which return false value.

发布评论

评论列表(0)

  1. 暂无评论