This link has been bookmarked by 22 people . It was first bookmarked on 27 Dec 2006, by Adam Skinner.
-
27 Aug 09
-
08 Jun 09
Lourens KeyserJavascript examples
-
08 May 09
-
28 Apr 09
-
17 Apr 09
-
10 Apr 09
-
09 Apr 09
-
17 Jan 09
LiteIntroduction
This document is a list of best practices and preferred ways of developing javascript code, based on opinions and experience from many developers in the javascript community. Since this is a list of recommendations rather than a list of absolute rules, experienced developers may have slightly differing opinions from those expressed below.
Table Of Contents
1. Always Use 'var'
2. Feature-Detect Rather Than Browser-Detect
3. Use Square Bracket Notation
4. Avoid 'eval'
5. Reference Forms and Form Elements Correctly
6. Avoid 'with' Statements
7. Use onclick In Anchors Instead Of javascript: Pseudo-Protocol
8. Use The Unary + Operator To TypeConvert To Number
9. Avoid document.all
10. Don't Use HTML Comments In Script Blocks
11. Avoid Cluttering The Global Namespace
12. Avoid sync "Ajax" calls
13. Use JSON
14. Use Correct <script> Tags
Always Use 'var'
Variables in javascript either have global scope or function scope, and using the 'var' keyword is vital to keeping them straight. When declaring a variable for use either as a global variable or as a function-level variable, always prefix the declaration with the 'var' keyword. The example below highlights the potential problem caused by not doing so.
Problem Caused By Not Using Var
var i=0; // This is good - creates a global variable
function test() {
for (i=0; i<10; i++) {
alert("Hello World!");
}
}
test();
alert(i); // The global variable i is now 10!
Since the variable i inside the function was not declared as a function-level variable by using the 'var' keyword, it references the global variable in this example. It is a good idea to always declare global variables using 'var', but it is vital to declare function-scoped variables using 'var'. The two approaches below are functionally identical.
Fixed Function
function test() {
var i=0;
for (i=0; i<10; i++) {
alert("Hello World!");
}
}
Fixed Function
function test() {
for (var i=0; i<10; i++) {
alert("Hello World!");
} -
13 Jan 09
-
if it returns true, then the HREF of the anchor will be followed like a normal link. If it returns false, then the HREF will be ignored. This is why "return false;"
-
-
10 Dec 08
-
18 Aug 08
-
31 May 08
-
06 May 08
-
18 Mar 08
-
26 Oct 07
-
08 May 07
-
06 May 07
Gavin AllanThis document is a list of best practices and preferred ways of developing javascript code, based on opinions and experience from many developers in the javascript community.
-
27 Dec 06
-
With dot notation, the property name is hard-coded and cannot be changed at run-time. With bracket notation, the property name is a string which is evaluated to resolve the property name. The string can be hard-coded, or a variable, or even a function call which returns a string property name. -
Prefixing a variable or expression with + will force it to evaluate as a number, which can then be successfully used in a math operation.
- 1 more annotations...
-
-
The javascript code that runs within the onclick handler must return true or false (or an expression than evalues to true or false) back to the tag itself - if it returns true, then the HREF of the anchor will be followed like a normal link. If it returns false, then the HREF will be ignored. This is why "return false;" is often included at the end of the code within an onclick handler.
-
-
-
22 Dec 06
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.