Monstre…way to go…a simple return statement!
By the way, I tried this out on Netscape, and it appears that Netscape (at least v. 4.7) doesn’t like it, still. Although the “return false” from the onClick is correct and part of the specifications in Javascript, I believe.
On that note, there isn’t much rhyme or reason or consistency to the “return” values from the event handlers. Some event handlers have a value you can return to do something (like in this case), and some don’t. onClick just happens to be one of the more common ones people use, so this one is good to know.
Well, I’d start by saying that it looks like pretty poor coding to me. I tested a few things out, and it appears that if you simply create a variable (without declaring it with “var”), it is global. This would explain why the scope issue is not a problem.
Now, in that “jump” function, try starting out the “loc” variable with a proper declaration:
var loc;
Then, you should get an error from the “land” function, since “loc” would be considered a local variable of “jump” only. (I got a script error when I tried this).
However, as it is, this variable was simply used without a declaration – and I didn’t realize this before, but it looks like this creates it automatically as a global variable. I tested this out by throwing some alert statements in the functions to print out the values of the variables (in pop-up boxes) during the processing of the functions. You could try this too. Example:
alert('loc = ’ + loc);
alert('ref = ’ + ref);
So, if “loc” is global, then there really was no need for the first parameter on the “land” function (the parameter “ref” – which is never actually used in the function). So regardless of the intent, it’s still poor and understandably confusing coding.
So, where did you find that code, matt?