Web pages are a streaming format, and Javascript streams right along with it. If you reference a function or an object in a javascript file that hasn’t been downloaded yet, or a part of the DOM that doesn’t exist yet, then you are going to see an error or problematic behavior.
Not escaping line broken strings
In Javascript if you break a line with a newline in a string, you have to escape it with a backslash. closing the string and reopening it with quotes may work better on some browsers.
Debugging with alert
There is a better way to debug, either through a formal debugger such as Venkman or Firebug, or through logging console functions like Firebug and Safari’s console.log or the cross platform Jslog.
Using ’+’ and forgetting about type
In Javascript the + operator means both concat strings and add numbers. Sometimes when a string is a number, it can get tricky. In Javascript 1 + ‘1’ = 2, but ‘1’ + ‘1’ = ‘11’.
Not specifying callback functions correctly.
A callback function is a reference to a function that will be invoked at some later point, such as when an ajax request returns. One common error is indicating the callback function with parens, callbackFunction() – this will execute the function immediately, and not on callback, although if you return a function this way, that should work too.
The idea of a callback is to specify either a function by name, or a string to eval.
Intialize variables
While FireFox or Mozilla browsers don’t care so much about referencing undefined variables, IE will trigger a javascript error if you reference or set a variable that has not been defined.
Before you use foobar, set var foobar;.
also in case of dom handling always call your java script at the end of page just for avoding any error