Top 5 Mistakes to Avoid In JavaScript

Instead of the usual Top Tips article, I thought I would look at the top 5 goofs for a change, and how to avoid them. I chose them purely because upon reading over what other coders were experiencing, I could see that I was not the only one – by far. To avoid repeating the errors, it’s all about practice makes perfect and these should be high on your radar.

  • Your code as well as your content should be optimized for search engines. On-page optimization should not exclude the code. Search engines do not crawl JavaScript or DHTML menus. Plan your navigation using different methods such as CSS menus. You could use a noscript tag for the links but it’s better to use CSS or plain HTML text menus.
  • You may be really at the end of your tether with people who like to steal your content. As a result, you may have disabled right clicks on your pages as can be accomplished with this code from hypergurl.com:

<script language="JavaScript1.1">
// distributed by http://www,hypergurl.com
<!—
var debug = true;
function right(e) {
if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2)) return false;
else if (navigator.appName == 'Microsoft Internet Explorer' && (event.button == 2 ||
event.button == 3)) { alert('This Page is fully protected!'); return false; } return true; }
document.onmousedown=right;
if (document.layers)
window.captureEvents(Event.MOUSEDOWN);
window.onmousedown=right;
//-->
</script>

This can annoy visitors whom cannot select the text for research purposes. Not all your visitors are thieves.

  • Check your blocks of code for any syntax errors and always repeat cross browser checks. Don’t take this for granted; check the views using various browsers for any JavaScript errors. These errors can prevent important script functions from running and your viewers losing sight of some of your page functionality. Use a service like http://browsershots.org/ to view your pages in multiple browsers and versions.
  • Avoid using evaluate() , use proper array structures instead. Too many instances of evaluate() can slow the script considerably.
  • If the errors produced are discovered to be non-browser specific but rather global, it’s usually down to some bad code. Normally, this is a typo or a missing function. A common one is not declaring variables using var. Various problems can arise if the variable is not meant to be global.

e.g. for(num=0; num<100; num++) {

This needs to be written as a formal declaration: for(var num=0; num<100; num++) {

Another example is omitting the semi-colon at the end of each JavaScript line. It’s not good practice.

I would be interested to know what people consider to be avoidable JavaScript issues. There are so many that can cause problems though most can be avoided with some care.

By Stephen Davies

Stephen Davies MSc is a content writer/copywriter and director of clickforseo.com, which offers SEO writing and marketing services to the SMB market.

Leave a comment