Online Documentation Server Web
Net technology
Web technology
Data bases
Other docs



. , - - , .

Previous Table of Contents Next

Example 16-1 (ex16-1.htm). A simple ciphering script.

Warning:  Do not count on this script for securing your data. An average high school graduate can beat the system within a few minutes. (If you read the chapter about operators you should be able to do so yourself.)

The first global statement initializes the variable list with a string consisting of all supported characters.


At first, the function prompts the user for the string he or she wants to encode. It is stored in the variable str. If the value of the variable is a Boolean false, the function, and actually the script itself, is terminated. The motivation behind terminating the function is that a Boolean false value can only be the result of the user pressing Cancel. A for loop is then used to check that all characters of the string are also located in the string held by list. The loop iterates through every character of the input string. Take a look at the condition used to test if the character is supported:

list.indexOf(str.charAt(i)) == 1

str.charAt(i) is the character for which the loops block is currently being executed. The variable i starts at 0, the index of the strings first character, and is incremented each time until it is equal to the index of the strings last character. Suppose the current character is t. The condition looks like this then:

list.indexOf("t") == 1

If the character t is not found in the string list, the method indexOf() whose argument is t returns 1exactly the number against which the returned value is tested.

If a character is not valid (not found in list), a message is displayed and the function is terminated, indirectly causing the scripts execution to end.

The function then asks the user to enter the key number, which must be an integer between 1 and 63. Because this is just an example, the input value is not tested. If the user clicks Cancel, the function is terminated. Otherwise the function continues, and the key number is converted from a numeric string to a number via the parseInt() function. The encoded string, which is returned by the function encode(), is displayed.

encode(str, key)

At first, an empty string is assigned to the variable code. A loop is used to replace every character of the input string by another character. The index of the current character (the one whose index in str is equal to the loops counter, i) in the list string is assigned to the variable ind. The bitwise OR operator is given the key number and the value of ind as operands. The character whose index in list is equal to the value returned by the bitwise OR operation is the one used to replace the current character in the new string, so it is assigned to the variable that holds the encoded string. The new string is returned by the function.

Global Statements

The only global statement is the one that calls the encipher function.


Strings are one of the most important data types in JavaScript. JavaScript tends to organize its elements in the form of objects, so all string-related functions and data are grouped together to form the String object. In this chapter we discussed the methods of the String object, as well as its single property, length. Because strings are direct or indirect (based on the way you create them) instances of this object, you can create prototypes to extend the objects capabilities. The JavaScript example provided at the end of the chapter gives a clear view of the String objects strength. We did not provide many examples in this chapter because string manipulation and handling can be found in almost every example further on in this book.

Previous Table of Contents Next

With any suggestions or questions please feel free to contact us