Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  14] [ 3]  / answers: 1 / hits: 100063  / 14 Years ago, tue, august 17, 2010, 12:00:00

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.


More From » jquery

 Answers
95

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 both preventDefault and stopPropagation, 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 the location.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 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).

  • Another possibility is <input type=image>. This has the accessibility advantages of the button with full visual control, but only for pure image buttons.


[#95892] Friday, August 13, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brendaw

Total Points: 508
Total Questions: 90
Total Answers: 100

Location: Maldives
Member since Sat, Jan 29, 2022
2 Years ago
;