View Full Version : Firefox Script Problem: resets page
SnoringFrog
03-09-2009, 03:34 PM
http://javascriptkit.com/script/scri...alendar.shtml#
I'm trying to use the calendar found on that page, but when the month/year is changed in Firefox, the page resets to the top (assuming the user has scrolled down the see the calendar). I do not want it to do this, is there something in the code that causes this?
RysChwith
03-10-2009, 08:21 AM
Can't follow the link, but I can take a guess. Changing the month or year involves clicking a link, yes? When that happens, the default action is to reload the page (or follow the link's URL), which causes it to reset to the top. You need to cancel the default action when the link is clicked.
If your function is called from an onclick attribute (<a onclick = "...">) then this is pretty easy. Just add the part in red:<a href = "#" onclick = "yourFunction(); return false">If it's not in an attribute, it gets a bit trickier.
Rys
SnoringFrog
03-10-2009, 01:15 PM
Thank you that solved the problem! Can you explain why this works? I'm glad to know it does, but I'd rather know what I was accomplishing.
RysChwith
03-11-2009, 08:24 AM
By default, clicking on a link sends the browser to the designated URL. The onclick handler is in addition to that action, not in place of it. So you have to explicitly tell the browser in some fashion to not take the default action; that's what the "return false" does.
In the case of your link, that URL is "#." Since there's no filename, it goes to the current page, then looks for the designated anchor... except that there's no anchor name designated, so it just goes to the top of the page. It's a common tactic for creating a link that doesn't actually go anywhere.
Make sense?
Rys
SnoringFrog
03-11-2009, 01:05 PM
So the default action would be a 'return true', correct? Thus, if no instruction for an alternate action is given (like the 'return false') it just goes ahead with the normal, 'true' action and reloads the page?
RysChwith
03-12-2009, 08:16 AM
Not really. The default action is to follow the link. I suppose you could think of the "return false" as sort of the magic word to stop it. It doesn't really map onto the boolean concept as you're thinking.
The way you're thinking about it is functionally accurate, though, so feel free to leave it at that. :)
Rys
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.