String Applications in JS
<font color="#ff0000">WebTV JS "Bug" - Reload Page (cmd-r)</font>



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:

<html>
    <head>

      JS/HTML Here
      (in blocks)

   </head>
<body>

      JS/HTML Here
      (in blocks)

</body>
</html>
When using this format, any HTML contained within "script" tags goes unrecognized. Both must be coded as independent entities, though variables, for example, can be passed from one to the other. All previous examples have done this. Most documents make use of this format.

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.

HTML


An example <br>to view

An example
to view.


JS


<script>
document.write("An example")
document.write("to view.")
</script>




J(HTML)S


<script>
document.writeIn("An example"+'<br>'+" to view")
</script>



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.,

document.writeln
('<b>'+"Bold"+'</b>')
document.writeln
('<u>'+"Underline"+'</u>')
document.writeln
('<font size=6>'+"size 6"+'</font>')
document.writeln
('<font color="#00ffff>' "+ "#00ffff"+'


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:

Lower to Upper Case:

    var up="All letters to Upper Case";
    up=up.toUpperCase(this);


Upper to Lower

    var low="ALL LETTERS TO LOWER CASE";
    low=low.toLowerCase(this);


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 . The argument of the function is said to be "this", implying the variable before the function. Otherwise, the function can be used with just the string variable in quotes as the argument. The more general form has been given. These are simple, yet instructive, functions to program as an excercise. The way the example has been done can be gotten by viewing source for this page.

Two other useful functions are those for determining string length and for selecting out only a portion of the string. For example:


var beg=3;
var word="An example to try";
len=word.length;
substr=word.substring(beg,len);
document.writeln(word," ",len," ",substr);


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:

substr=word.substring(0,9);


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

<< PreviousNext >>