An HTML document can be viewed as just one long string or as a collection of shorter strings, i.e., substrings. Numbers, when they occur, are also strings and must be converted to a numeric form if they are to be used in calculations. Here the various ways that strings can be utilized in a document containing JS will be examined. Document StructureUse of JS in a document now means that there are three formats possible: all HTML, all JS, or a combination of the two. The combined form is what has been used thus far and has the generalized structure:
Occasionally an all JS structure is used, and when it is used, HTML coding is modified so it can be displayed. The generalized structure is the same as that above only now the JS can be viewed as embedded in the HTML, or the converse. It is convenient to invent an abbreviation "J(HTML)S" to represent this format. The distinction between the two can be seen in trying to write a simple string variable.
Though not seen, giving rise to each phrase beneath the code shown is the code itself in operation. Some things are worthy of note from this comparison. For the JS one, it does not matter whether one document.write(string) or two are used. The result still appears as a string on one line. Putting a "<br>" between two of them is no help, as HTML tags are ignored and will not be seen even if there. The desired line break could be gained by putting a "<br>" between them if each was separately scripted, but this is somewhat labor-intensive for what is gained. With the J(HTML)S variation, though, a "<br>" is concatinated into the string. Single quotes are used as this is separate from each of the strings. This does give rise to the desired line break, but only if document.writeIn(string) is used. In this I is a small l and not a capital i. Doing the same with document.write(string) has the entire string, including the <br>, printed on the same line. Using HTML Color & Size The last observation is an important one, for it shows how it is possible to modify font size, color, or face (when possible) of text when using JS. The tag is concatinated-in analogously to how they are used in HTML, e.g.,
With care large sections of HTML can be introduced into a JS document. It is usual, though, to keep the HTML and JS separate to take full advantage of their coding capabilities. String FunctionsTwo string functions have already been used: parseFloat and parseInt. These take numbers input as strings and convert the strings to numerics so that mathematical operations can be performed. Among the other string functions, the ones that will be found most useful in general are those that convert from lower to upper case, or the converse, and the ones that give the string length or a substring, i.e., a part of the original string value. Case conversion of a string may be done as follows:
Application of these may be illustrated using pop-up boxes as input-ouput. In these a variable is first defined for the string. This variable is placed before the function, as with Two other useful functions are those for determining string length and for selecting out only a portion of the string. For example:
In this, "len" is the number of characters, a numeric integer, in the string whose value is "An example to try". The substring function is used to define a new string whose values are the characters beginning at position 3 and ending at position 17, the length of the string variable. The resulting substring is "example to try". It should be remembered that counts in JS begin with 0. To have started at the beginning of the variable "word" and gone to position 9 would be written: The substring obtained in this instance is "An example". Commas are used to separate variables when there is more than one variable in document.write(var1,var2,var3) or document.writeln(var1,var2,var3). JS Test-Bed 1 - - JS Test-Bed 2
|