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