Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
1145
JavaScript Guidelines
Introduction
This section describes some rules and guidelines, which you
should know when developing and writing code with JavaScript.
Case-Sensitive
JavaScript is a case-sensitive language. That means if you
declare a variable as “x” and use it as “X” then you wont see any output. “X”
and “x” are both different variables. The same is valid for functions. If you
define a function with the name “GetValues” and call it with the name
“Getvalues”, then you wont see any processing. Both are different functions. So
always be aware how you call your objects, variables or functions. Scripting
languages like VBScript doesn’t make any difference between that. VBScript is
not case-sensitive.
Symbols
All opened symbols must be closed properly. If you started a
string with " then you should also close it with ". If you don’t do
this, the page wont executed correctly. Other symbols are:
Symbol:
Used:
Example:
{}
This is mostly used in functions or in condition
statements.
function x()
{
}
[]
This is used in arrays.
var arTest = new Array(2)
arTest[0] = "string1"
arTest[1] = "string2"
arTest[2] = "string3"
""
This is usually used in strings.
var mystr = "this is a string."
''
This can be also used in strings.
var mystr = 'this is a string'
Special Characters in Strings
You can easily use special characters in a string. Special
characters are for example ", & etc. To display these characters in a
string, you just need to use a \ in front of the character and it will display
the special character.
NoScript
Note that some users don’t allow or don’t want to run a
JavaScript on the browser. So of the users maybe don’t know that JavaScript is
disabled or they might use a browser, which doesn’t support it. In that case
you should always use the <noscript> tag to displays a message that
JavaScript is either disabled or not supported by the browser.
Variables and Functions
Don’t use names for variables and functions, which doesn’t
say or mean anything. You may need to take a look at the code after years and when
you have variables defined, like: x, y, z, then you won’t remember why you
created them and what the usage was. Always use names, which are useful for the
task. For example, when you can create a variable to store a welcome message
then you could call it: strWelcomeMsg. As you can see that name says more then
just x, y or z. The same is valid for functions. For example you could create a
function, which displays a welcome message and call it: DisplayWelcomeMsg().
Comments
Always comment your code with the necessary information.
Some coders always add a date, time and the last updated action, name etc. as a
comment to the file. That way they will always know when the last time the file
was update and which function was edited. Furthermore they contain also the
name of the person, who did the update. Comments can be either created in one
line with // or in multiple lines with /*…*/. The following example shows it:
// Please comment only this line
/* Everything between these
signs, will be
commented out.
*/
Debugging
When you encounter problems in your code, and need to debug
it. Then you can make use of the alert message box. It is very handy and it
will show you the current value of the passed variable.