Here are some useful javascript tips I have learned:
A HREF and JAVASCRIPT
I have found that there are so many standards for using a text link to call just a javascript function, that few people actually no the proper way to write this code. Here are some examples of what I constantly see everywhere, and they are all bad and incorrect.
1) <a href="#" onClick="callFunction()"> Text Link </a>
The problem here is that when the link is clicked, in addition to the callFunction() being called, the href '#' will send the focus of the page to the top. If the link is located at the top of the page already, then this will work fine. Since many links are already like this, it goes unspotted by web designers. If you want the focus of the screen to remain the same though, this will not work.
2) <a href="javascript:callFunction();"> Text Link </a>
Invoking the 'javascript' protocol command inside the href tag can also seem like the right way, but in fact it does not work in Firefox. In Internet Explorer, you get the right functionality, but in Firefox the function will actually get called again when the mouse is clicked anywhere else on screen. So the callFunction() will be called twice, and this will never be good.
3) <a href="" onClick="callFunction()"> Text Link </a>
This clearly will not work because after the callFunction() is called, the href is empty, which causes the page to either be directed to the root page, or to refresh the page, depending on the browser. In all cases though, this method is bad.
Finally, the SOLUTION:
<a href="#" onClick="callFunction();return false;"> Text Link </a>
This method works in all browsers because the browser knows to ignore the href command. The browser first calls the callFunction(), then it moves on, sees 'return false', and stops. No other processing is done, and the browser is finished with the Text Link. If you have a toggle between visible and hidden on a particular element, and the element is somewhere in the middle of the web page, this format is a must.
Please Note: There are many other ways to create links like this, mostly bad, but I have given information about the most common.