When I want some link to not do anything but only respond to javascript actions what's the best way to avoid the link scrolling to the top edge of the page ?
I know several ways of doing it, they all seem to work fine :
<a href="javascript:void(0)">Hello</a>
or
<a id="hello" href="#">Hello</a> <script type="text/javascript> $(document).ready(function() { $("#toto").click(function(){ //... return false; }); }); </script>
and even :
<a id="hello" href="#">Hello</a> <script type="text/javascript> $(document).ready(function() { $("#toto").click(function(event){ event.preventDefault(); //... }); }); </script>
Do you have any preference ? why ? in which conditions ?
PS: of course the above examples assume you're using jquery but there's equivalents for mootools or prototype.
preventDefault() prevents the default browser behavior for a given element. stopPropagation() stops an event from bubbling or propagating up the DOM tree. Whereas, return false is a combination of both preventDefault() and stopPropagation() .
e. preventDefault() will prevent the default event from occuring, e. stopPropagation() will prevent the event from bubbling up and return false will do both. Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably, return false does not stop the event from bubbling up.
Another alternative to JavaScript void 0 is to use return false. When the click returns false, the browser will not take any action.
Use preventDefault(); if you want to “just” prevent the default browser behaviour. Use return false; when you want to prevent the default browser behaviour and prevent the event from propagating the DOM. In most situations where you would use return false; what you really want is preventDefault() .
Binding:
javascript:
URLs are a horror to be avoided at all times;Responses:
return false
means both preventDefault
and stopPropagation
, so the meaning is different if you care about parent elements receiving the event notification;preventDefault
/stopPropagation
have to be spelled differently in IE usually (returnValue
/cancelBubble
).However:
<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.#thatelementsid
and use unobtrusive scripting to grab the element ID from the link name. You can also sniff the location.hash
on document load to open that element, so the link becomes useful in other contexts.<input type="button">
or <button type="button">
. You can style it with CSS to look like a link instead of a button if want.<span>
instead. You can add a tabindex
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).<input type="image">
. This has the accessibility advantages of the button with full visual control, but only for pure image buttons.The only advantage that I can think of to using javascript:void(0)
is that it will be supported even by the oldest browsers. That said, I would use one of the other unobtrusive approaches you have mentioned:
event.preventDefault()
and return false
can be used interchangeably.event.preventDefault()
will prevent the page from reloading, as desired, but will allow the click event to bubble up to the parent. If you want to stop the bubbling, you can use it in conjunction with event.stopPropagation
.return false
will additionally stop the event from bubbling up to the parent.I say 'interchangeably' in the first point above because much of the time we do not care whether or not an event bubbles up to the parent(s). However, when do we need some fine-tuning, we should consider points two and three.
Consider the following example:
<div>Here is some text <a href="www.google.com">Click!</a></div> $("a").click(function(e) { e.preventDefault(); }); $("div").click(function() { $(this).css("border", "1px solid red"); });
Clicking on the anchor will prevent the default action of the event from being triggered, so the browser will not redirect to www.google.com. However, the event will still 'bubble up' and cause the div's click event to fire, which will add a border around it. Add e.stopPropagation()
or just return false
and the div's click event will not fire. You can mess with it here: http://jsfiddle.net/cMKsN/1/
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