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:
<a href="javascript:void(0)">
- It seems like a bit too much code for just being empty<a href=#>
- If I don't want to move to another page, I must return false in the JavaScript call<a href>
- This option breaks the cursor and hover style and the browser doesn't render it as a link<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:
<a href="javascript:void(0)">
- It seems like a bit too much code for just being empty<a href=#>
- If I don't want to move to another page, I must return false in the JavaScript call<a href>
- This option breaks the cursor and hover style and the browser doesn't render it as a link<a>
- idem
What is the best href
value for empty anchors? I'm not interested to keep functionality without JavaScript
- 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
6 Answers
Reset to default 3The 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.