JavaScript Tip of the Week for May 20, 1996: All about Windows (no, not the OS) | 2 | WebReference

JavaScript Tip of the Week for May 20, 1996: All about Windows (no, not the OS) | 2

"); text = (text +"

Mark Anthony's Address from
'Julius Caesar'


"); text = (text + "Friends, Romans, " +form.input1.value+ " lend me your " +form.input2.value+ "; "); text = (text + "I come to " +form.input3.value+ " Caesar, not to praise him. "); text = (text +"The evil that men do lives after them, the good is oft interred with their " +form.input4.value+ "; "); text = (text +"so let it be with " +form.input5.value+ ". " ); text = (text +"The noble Brutus hath told you Caesar was " +form.input6.value+ "; if it were so, it was a grievous fault. "); text = (text +"If you have " +form.input7.value+ ", prepare to shed them now. "); text = (text +"You all do know this " +form.input8.value+ ". "); text = (text +"I remember the first time Caesar put it on. "); text = (text +"Through this well-beloved Brutus stabbed; for Brutus, as you know, was Caesar's " +form.input9.value+ ": "); text = (text +"this was the unkindest " +form.input10.value+ " of all. "); text = (text +"Here is the " +form.input11.value+ ", under Caesar's seal. To every Roman " +form.input12.value+ " he gives, "); text = (text +"to every several man, seventy five " +form.input13.value+ ". "); text = (text +"Here was a/an " +form.input14.value+ "! When comes another?"); msgWindow=window.open("","displayWindow","toolbar=no,width=375,height=480,directories=no,status=no,scrollbars=yes,resize=no,menubar=no") msgWindow.document.write(text) msgWindow.document.close() } } // -->

JavaScript Tip of the Weekfor May 20, 1996: Using forms and windows together

That's all great, but where's the fun in windows? Add forms and windows suddenly become more useful; I have used a "Madlibs" like example to show this. Quickly fill in the form, and then press the button to see how it all works.

Plural Noun:
Part of body (plural):
Verb:
Name:
Name of Polictician:
Adjective:
Article of Clothing:
Article of Clothing:
Occupation:
Noun:
Noun:
Noun:
Animal (plural):
A Food:



Neat, huh? You all know how to create a window by now but do you know how to write to a window, and send form data to a specific window? First you have to create a form and give each object in the form a name. In this example, there are 14 input boxes, each named input1, input2, etc. After the user fills in the boxes, the data is sent to a function by pressing the Create Window button. The code for this is simple:
    <INPUT TYPE="button"
           VALUE="Create Window"
           onClick="create(this.form)">

The code sends the form data to the create function, which then puts all the form input data and text into a long string variable text. Now this is where many people may run into a problem. For some yet unkown reason, JavaScript will return an "undetermined string literal" error message if a string is too long, say 254 characters or more. Now there's any easy fix for this. Just split up the string into smaller parts with addition signs, which you have to do anyway because of the form inputs:
    text = ("Friends, Romans, "  + form.input1.value +  " lend me your "
            + form.input2.value +  "; ");
   

Now you must create a blank window. Then take the string text and write it to that window:
    msgWindow=window.open("", <-- Notice a URL is not required
                          "displayWindow",
                          "toolbar=no,
                          width=375,
                          height=480,
                          directories=no,
                          status=no,
                          scrollbars=yes,
                          resize=no,
                          menubar=no")
    msgWindow.document.write(text)
    msgWindow.document.close()

As you can see, a simple document.write is all that you need to write the data in text to the window, but you do have to refer to the window by name. I hope you can find some more creative uses for windows and frames than I have, because that's the beauty of letting everybody in on HTML and JavaScript programming. Source