This link has been bookmarked by 190 people . It was first bookmarked on 27 Dec 2006, by Adam Skinner.
-
29 Apr 12
thetarbrethis is a list of recommendations rather than a list of absolute rules, experienced developers may have slightly differing opinions from those expressed below.
-
20 Mar 12
-
01 Mar 12
rfessler"Javascript Best Practices"
best practices javascript BestPractices practices js programming
-
08 Feb 12
-
06 Feb 12
-
31 Jan 12
-
yuukaa#javascript #bestpractices #javascripttoolbox #webdev
-
26 Jan 12
-
25 Jan 12
-
24 Jan 12
-
Matt Ritterde is written to detect browser versions and to take different action based on the user agent being used. This, in general, is a very bad practice. Any code which even looks at the global "navigator" object is suspect.
The better approach is to use feature detection. That is, before using any advanced feature that an older browser may not support, check to see if the function or property exists first, then use it. This is better than detecting the browser version specifically, and assuming that you know its capabilities. An in-depth article about this topic can be found at http://www.jibbering.com/faq/faq_notes/not_browser_detect.html. -
23 Jan 12
-
07 Dec 11
-
19 Nov 11
-
13 Oct 11
-
30 Sep 11
-
29 Jul 11
-
30 Jun 11
-
19 Jun 11
-
14 Jun 11
-
13 Jun 11
-
12 Jun 11
-
11 Jun 11
-
10 Jun 11
-
19 May 11
-
07 Apr 11
-
17 Mar 11
-
15 Feb 11
-
19 Jan 11
-
10 Oct 10
-
28 Sep 10
-
23 Aug 10
-
17 Aug 10
-
16 Aug 10
-
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 Varvar 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 Functionfunction test() { var i=0; for (i=0; i<10; i++) { alert("Hello World!"); } }Fixed Functionfunction test() { for (var i=0; i<10; i++) { alert("Hello World!"); } } -
Use onclick In Anchors Instead Of javascript: Pseudo-Protocol
When you want to trigger javascript code from an anchor <A> tag, the onclick handler should be used rather than the javascript: pseudo-protocol. 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.
Correct Syntax<a href="javascript_required.html" onclick="doSomething(); return false;">go</a>
-
-
03 Aug 10
-
06 Jul 10
-
01 Jul 10
-
26 Jun 10
Hyung-Joo LimJavascript Best Practices http://ow.ly/17TceE
– Javascript News (del_javascript) http://twitter.com/del_javascript/statuses/17054948098 -
26 May 10
-
02 Apr 10
-
11 Feb 10
-
20 Dec 09
-
19 Dec 09
-
09 Dec 09
-
27 Aug 09
-
18 Jul 09
-
17 Jul 09
-
08 Jun 09
Lourens KeyserJavascript examples
-
08 May 09
-
28 Apr 09
-
17 Apr 09
-
10 Apr 09
-
02 Mar 09
-
18 Feb 09
-
12 Feb 09
Marg WilkinsonThis 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, ex
-
08 Feb 09
-
20 Jan 09
-
19 Jan 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!");
} -
16 Jan 09
P eterThis 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, ex
best-practices practices js javascript webdevelopment bestpractices programming
-
15 Jan 09
-
14 Jan 09
-
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;"
-
-
04 Jan 09
Marek RozanskiThis 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.
-
10 Dec 08
-
18 Aug 08
-
31 May 08
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.