I’m officially rubbish at javascript. I just spent about an hour trying to figure out why my nice Jquery slideToggle/slideDown functions were making the browser jump up to the top of the page. I wouldn’t normally write a blog post to tell the world how shit I am, but I recon I’m not the only developer who having declared javascript the devils tool back around 2001, only to pick it up again recently with great enthusiasm after discovering one of the nice new javascript frameworks that have salvaged it from our bin, is still a bit shit at javascript.
The Problem
I too wanted to use the tasty slideToggle type function that everyone and their dog uses on their website now a days:
$(document).ready(function() {
$("#crap").click(function (){
$("#crapDiv").slideToggle();
});
});
Resize and Scroll you window down a litte then Click me
Why the Problem
After poking around I realised the the browser is attemping to follow the link, and it’s something to do with this that’s causing the jump (hence the button example on the jquery site not being affected).
The Solution
return false:
$(document).ready(function() {
$("#good").click(function (){
$("#goodDiv").slideToggle();
return false;
});
});
Resize your browser scroll down and Click me
Moral of the story
Errr, don’t be crap!

0 Responses to “Jquery browser window jumping fun”