HTML and CSS Reference
In-Depth Information
When this command is run, the value of the CalcArea() function is returned, mul-
tiplied by 2, and then stored in the z variable. Using the above parameter values, the
value of the z variable is 96.
Functions and Variable Scope
As you've seen, the commands within a function are run only when the function is called.
This has an impact on how variables within the function are treated. Every variable you
create has a property known as scope , which indicates where you can reference the
variable within the Web page. A variable's scope can be either local or global. A variable
created within a JavaScript function has local scope and can be referenced only within
that function. Variables with local scope are sometimes referred to as local variables .
In the function you created in this session, the emLink variable has local scope and can
be referenced only within the showEM() function. Parameters such as the userName and
emServer parameters from the showEM() function also have local scope and are not rec-
ognized outside of the function in which they're used. When the showEM() function stops
running, those variables and their values are not held in the computer memory and their
values can no longer be accessed.
Variables not declared within functions have global scope and can be referenced
from within all script elements on a Web page. Variables with global scope are often
referred to as global variables .
Accessing an External JavaScript File
You show your work on the staff directory to Kate. She's happy that you were able to use
JavaScript to generate the e-mail addresses, but she's still concerned that the text of each
employee's username and mail server are present in the document as parameter values of
the showEM() function. She would like to have those values hidden from any e-mail har-
vesters that might be scanning the document code. You discuss the issue with a program-
mer friend who sends you a file containing the following function:
functionƒstringReverse(textString)ƒ{
ƒƒƒifƒ(!textString)ƒreturnƒ'';
ƒƒƒvarƒrevString='';
ƒƒƒforƒ(iƒ=ƒtextString.length-1;ƒi>=0;ƒi--)
ƒƒƒƒƒƒrevStringƒ+=ƒtextString.charAt(i);
ƒƒƒreturnƒrevString;
}
Interpreting the code contained within this function is beyond the scope of this tuto-
rial, but for now it is sufficient to know in general what the function does. The function
has a single parameter named textString, which stores a string of characters. The func-
tion then creates a variable named revString that stores the characters from textString in
reverse order, and that reversed text string is returned by the function. For example, if
you called the function in the statements
userNameƒ=ƒstringReverse(“reldac”);
emServerƒ=ƒstringReverse(“vog.lpm”);
the userName variable would store the text string cadler , and the emServer variable
would store the text string mpl.gov (the text strings reldac and vog.lpm in reverse order).
You show this function to Kate and she agrees that this will be sufficient to hide the
actual username and server name from most e-mail address harvesters.
Search WWH ::




Custom Search