Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use `javascript:void(0)` instead of `javascript:` as an href do nothing placeholder? [duplicate]

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 seen href="javascript:;" Is there any reason I would not just use href="javascript:" instead?

like image 658
700 Software Avatar asked Mar 08 '11 19:03

700 Software


People also ask

Why do we use void 0 in Javascript?

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.

Which href value should I use for Javascript links or Javascript void 0?

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.

What is Javascript void in href?

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.

What is a href javascript void 0);?

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.


4 Answers

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.

like image 196
Spudley Avatar answered Oct 20 '22 16:10

Spudley


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:;.

like image 45
david Avatar answered Oct 20 '22 17:10

david


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.

like image 44
Jason Miesionczek Avatar answered Oct 20 '22 16:10

Jason Miesionczek


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.

like image 38
Rian Schmits Avatar answered Oct 20 '22 17:10

Rian Schmits