I have seen href="javascript:void(0)"
and I have seen href="javascript:;"
Is there any reason I would not just use href="javascript:"
instead?
Edit: Let me make it clear: I am combining this with an onclick
and have no objection to using return false
if it becomes necessary to use the alternative. Of course this is only if the alternative makes better sense over javascript:
.
Also, I have yet to see a answer to my question shown (clearly I think) in the first paragraph. Thanks, david. :)
I have seen
href="javascript:void(0)"
and I have seenhref="javascript:;"
Is there any reason I would not just usehref="javascript:"
instead?
JavaScript void 0 means returning undefined (void) as a primitive value. You might come across the term “JavaScript:void(0)” while going through HTML documents. It is used to prevent any side effects caused while inserting an expression in a web page.
The href= “” will only load the current page, while href= “#” scrolls the current page to the top, while href= 'javascript:void(0)' will do nothing at all. The same effects of javascript:void(0) are realized by returning false from the click event handler of the <a> tag with either of the two methods.
With void , it tells the browser not to return anything (or return undefined ). Another use case of links with the javascript:void(0) reference is that sometimes, a link may run some JavaScript code in the background, and navigating may be unnecessary.
Usage of javascript:void(0) means that the author of the HTML is misusing the anchor element in place of the button element. Anchor tags are often abused with the onclick event to create pseudo-buttons by setting href to "#" or "javascript:void(0)" to prevent the page from refreshing.
Personally I'd avoid using either. I'd have onclick='return false;'
instead, and just set href='#'
.
Why? Because if you put Javascript code in the href, then it appears in the browser's status bar when you hover over the link, just like any other URL. It just looks messy.
In fact, since it's going to be ignored anyway, you could put any URL you fancy into the href
, not just a hash. The hash is safest, just in case your user has Javascript disabled, but aside from that point, it could be anything.
But I have to point out: having an <a>
tag with the href going to a void Javascript code seems to rather defeat the point of having an <a>
tag in the first place. I can see how you'd want to enable and disable a link at runtime, but simply setting the href to void does seem somewhat pointless. If it isn't going to be a real link, why not just have a <span>
or some other tag. If you must make it look like a link, it's trivial to do that in CSS.
Doesn't answer you question but may shed a bit more light on it, some early versions of browsers like netscape had problems when a script was used within the href
.
The void
operator was pretty much to only way force the click to do nothing.
Now, with browsers properly implementing "pseudo URLs", you can safely just use javascript:;
.
The ideal situation is to not put any code on the element itself, but rather use javascript in either a script tag, or in an external file and bind the event handler there.
It's better not to use either. The href
attribute indicates a url. It's better to use event handlers to get the desired behaviour, which is probably the onclick
event.
In this case the desired behaviour apparently is to do nothing. Firstly, why use an anchor tag at all when it doesn't link anywhere? Secondly, this can be handles by preventing event bubbling, by letting the onclick
event handler return false
. Then the href
can either point to #
, or better even to have it point to a url that more or less has the same effect as the event handler, so it will degrade gracefully when javascript is turned off.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With