1. Computing
Send to a Friend via Email

Improving Your JavaScript

JavaScript has evolved over the years and there are things that can be done with JavaScript now that couldn't be done a few years ago. These tutorials present ways in which you can take existing JavaScript from a while ago and rework them to make better use of the newer features that are now available.

The Best Place to Attach JavaScript
There are only two places worth considering for attaching modern JavaScript unobtrusively into a web page.

Cross Browser AddEvent and RemoveEvent
This variant on adding and removing event listeners resolves some of the cross browser issues.

href="#"
A # by itself in the href attribute is not valid. Sites teaching you how to write HTML and JavaScript use it as a placeholder that should be replaced in live code with a real destination.

Self Documenting Code
You should not be using comments to describe what your code does, the code itself should show that.

New JavaScript Commands
Sometimes new commands are added to JavaScript. Older browsers may not be able to process these commands.

Getting Rid of document.write
The document.write statement was an essential part of early JavaScripts but is now completely unnecessary as anything that can be done with document.write can be done better using innerHTML.

Accessibility and JavaScript
JavaScript should be coded in an unobtrusive fashion in order to make our web pages more accessible to those visitors who don't have it enabled.

Making Your JavaScript More Efficient
As JavaScript is an interpreted language there are minor changes to the way we write out code that will affect how quickly it will run.

Unobtrusive JavaScript - Avoiding Script Clashes
There is more than one meaning when it comes to describing JavaScript as unobtrusive. Here we look at how to stop one JavaScript interfering with another JavaScript on the same page.

Faster DOM Access
Enable your Javascript code to interact with the DOM before the entire page finishes loading with this simple code change.

Moving JavaScript out of the Web Page
How to separate the JavaScript in your web page from the HTML and put it in its own file.

Getting Rid of Noscript
Here are a couple of simple examples to demonstrate how you can make your page more accessible, more semantically correct, as well as making the JavaScript completely unobtrusive simply by getting rid of the noscript tag.

Alternatives to eval
The javascript command "eval" can be used to evaluate text as javascript. Doing so though opens up major security holes in your code as well as making the code way less efficient than it needs to be. There is (almost) never any need to use this function as for most uses there is an alternate coding that is cleaner and more efficient.

Table Specific DOM Elements
There are two ways to access the elements within a table from JavaScript. I had always assumed that one of those ways was always faster than the other. It isn't in all browsers but by running a speed test I was able to come up with an actual comparison of the two methods so that I no longer need to assume that one is faster than the other...

HTML 5 And noscript
HTML 5 proposes changing where the noscript tag can be used so as to reintroduce a use for it.

document.write
It may be the easiest way to write something into your web page but document.write has a lot of limitations on how it can be used.

Evolving JavaScript
What JavaScript can and can't do has changed over time. What was best (or only) practice at one time may now be something to avoid.

Unobtrusive JavaScript
Your web page will work a lot better with the JavaScript completely removed from the HTML. Here's how to take all the JavaScript out of the body of your web page and still have it work just the same.

Design for Usability
Considerations for making a web page as accessible and user friendly as possible.

Making Events Unobtrusive
Update old JavaScripts by converting them to make them unobtrusive. Here's step by step instructions on how to do it.

©2014 About.com. All rights reserved.