Web Weenie Posted February 25, 2010 Share Posted February 25, 2010 I have a js the uses an onclick to swap an image. It causes the page to jump back to the top of the page. I've tried to add a return false at the end of the script but this doesn't fix the problem. How can I prevent this effect from happening. Am I placing the return false incorrectly? or is there something else i need to do?TIA Link to comment Share on other sites More sharing options...
student101 Posted February 25, 2010 Share Posted February 25, 2010 post your onclick code, as it is... Link to comment Share on other sites More sharing options...
jeffman Posted February 25, 2010 Share Posted February 25, 2010 What element has the click handler assigned to it? I'm thinking something like <a href="#" onclick="somefunc()"> ? If so, it's not the click. It's the href. "#"+value is used to jump the user to an internal anchor. Just "#" jumps the user to the top.Where you put the return value matters. Did you try: <a href="#" onclick="somefunc(); return false;"> ?Anyway, using links just to trigger javascript functions is bad practice. It's okay if they are also real links. If not, put your onclick attribute in a span or paragraph.Apologies if I've guessed incorrectly. By all means post some code if this doesn't solve the problem. Link to comment Share on other sites More sharing options...
Web Weenie Posted February 25, 2010 Author Share Posted February 25, 2010 What element has the click handler assigned to it? I'm thinking something like <a href="#" onclick="somefunc()"> ? If so, it's not the click. It's the href. "#"+value is used to jump the user to an internal anchor. Just "#" jumps the user to the top.Where you put the return value matters. Did you try: <a href="#" onclick="somefunc(); return false;"> ?Anyway, using links just to trigger javascript functions is bad practice. It's okay if they are also real links. If not, put your onclick attribute in a span or paragraph.Apologies if I've guessed incorrectly. By all means post some code if this doesn't solve the problem.Ah-ha! I didn't think of doing it inline... that worked like a charm! Thanks . And to answer, there was no <a href>Thanks Link to comment Share on other sites More sharing options...
jeffman Posted February 25, 2010 Share Posted February 25, 2010 Yeah, if you're going to run inline javascript, the return statement has to be part of the inline code.If the element wasn't a link though, I can't imagine what made it jump. Satisfy my curiosity? Link to comment Share on other sites More sharing options...
Web Weenie Posted February 25, 2010 Author Share Posted February 25, 2010 Yeah, if you're going to run inline javascript, the return statement has to be part of the inline code.If the element wasn't a link though, I can't imagine what made it jump. Satisfy my curiosity?Well if there is a quirk to find, I can find it...The html that calls the js:<img src="images/scroll3.jpg" width="59" height="15" vspace="2" border="0" align="absmiddle" id="mybtn" onclick="changeimage(); return false;" /> Link to comment Share on other sites More sharing options...
jeffman Posted February 25, 2010 Share Posted February 25, 2010 And changeimage() by itself doesn't do it? And the image is not in a link? Weird. Link to comment Share on other sites More sharing options...
ShadowMage Posted February 25, 2010 Share Posted February 25, 2010 Could that possibly be a behavior of quirks mode? (I'm not 100% percent sure what that is but I've seen quirks mode being accused of a lot of weird things on these forums ) Link to comment Share on other sites More sharing options...
Web Weenie Posted February 26, 2010 Author Share Posted February 26, 2010 Could that possibly be a behavior of quirks mode? (I'm not 100% percent sure what that is but I've seen quirks mode being accused of a lot of weird things on these forums )I seem to live in quirks mode... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.