href=javascript: vs. href=javascript:void(0)
It does not cause problems but it's a trick to do the same as PreventDefault
when you're way down in the page and an anchor as:
<a href="#" onclick="fn()">click here</a>
you will jump to the top and the URL will have the anchor #
as well, to avoid this we simply return false;
or use javascript:void(0);
regarding your examples
<a onclick="fn()">Does not appear as a link, because there's no href</a>
just do a {text-decoration:underline;}
and you will have "link a-like"
<a href="javascript:void(0)" onclick="fn()">fn is called</a>
<a href="javascript:" onclick="fn()">fn is called too!</a>
it's ok, but in your function
at the end, just return false;
to prevent the default behavior, you don't need to do anything more.
Why use `javascript:void(0)` instead of `javascript:` as an href do nothing placeholder?
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:;
.
JavaScript - href vs onclick for callback function on Hyperlink
Putting the onclick within the href would offend those who believe strongly in separation of content from behavior/action. The argument is that your html content should remain focused solely on content, not on presentation or behavior.
The typical path these days is to use a javascript library (eg. jquery) and create an event handler using that library. It would look something like:
$('a').click( function(e) {e.preventDefault(); /*your_code_here;*/ return false; } );
javascript:void(0); vs return false vs preventDefault()
Binding:
javascript:
URLs are a horror to be avoided at all times;- inline event handler attributes aren't brilliant either, but OK for a bit of rapid development/testing;
- binding from script, leaving the markup clean, is typically considered a best practice. jQuery encourages this, but there is no reason you can't do it in any library or plain JS.
Responses:
- In jQuery
return false
means bothpreventDefault
andstopPropagation
, so the meaning is different if you care about parent elements receiving the event notification; - jQuery is hiding it here but
preventDefault
/stopPropagation
have to be spelled differently in IE usually (returnValue
/cancelBubble
).
However:
- You have a link that isn't a link. It doesn't link anywhere; it's an action.
<a>
isn't really the ideal markup for this. It'll go wrong if someone tries to middle-click it, or add it to bookmarks, or any of the other affordances a link has. - For cases where it really does point to something, like when it opens/closes another element on the page, set the link to point to
#thatelementsid
and use unobtrusive scripting to grab the element ID from the link name. You can also sniff thelocation.hash
on document load to open that element, so the link becomes useful in other contexts. - Otherwise, for something that is purely an action, it would be best to mark it up like one:
<input type="button">
or<button type="button">
. You can style it with CSS to look like a link instead of a button if want. - However there are some aspects of the button styling you can't quite get rid of in IE and Firefox. It's usually not significant, but if you really need absolute visual control a compromise is to use a
<span>
instead. You can add atabindex
property to make it keyboard-accessible in most browsers although this isn't really properly standardised. You can also detect keypresses like Space or Enter on it to activate. This is kind of unsatisfactory, but still quite popular (SO, for one, does it like this). - Another possibility is
<input type="image">
. This has the accessibility advantages of the button with full visual control, but only for pure image buttons.
Related Topics
How to Refresh Page on Back Button Click
Why Split the ≪Script≫ Tag When Writing It With Document.Write()
Determine If an HTML Element'S Content Overflows
JavaScript String/Integer Comparisons
How to Get a Number of Random Elements from an Array
How to Send a Json Object Using HTML Form Data
How to Store Arbitrary Data For Some HTML Tags
Stopping Gif Animation Programmatically
Encryption in JavaScript and Decryption With PHP
Invoking JavaScript Code in an Iframe from the Parent Page
Changing Website Favicon Dynamically
Is It Wrong to Place the ≪Script≫ Tag After the ≪/Body≫ Tag
Link Index.Html Client.Js and Server.Js
Appending HTML String to the Dom