The document object is associated with the contents of a window. It is a property of the window object and also is an object itself. It has properties and methods that permit JavaScript control over the look and feel of an HTML document. Document PropertiesSeveral of the properties of the document object are described in the following table:
These properties are accessible with a command in the form: document.property If the reference is to the current document, you can leave off the document. prefix; however, for good script documentation, you probably should include it. The location property provides a convenient way to navigate through a web. It is a reference to the browser's Location dialog box containing the URL of the current document. Therefore, if you need to link to a different document, just assign that document's URL to the location property:
These buttons work exactly like <A HREF=> links. In this case the name of the target document is simply assigned to the document.location property as in the following example coding from the first radio button:
<INPUT TYPE=RADIO
onclick = "document.location='p01.html' "
>1. The JavaScript Language Other useful document properties are illustrated with the following buttons:
Also, check out the following link to see how you can use the referrer property.
Using document.referrer Property (Netscape Navigator 3.0)
Document MethodsThe document methods relate to opening, closing, and writing to a document. They are summarized in the following table:
In earlier examples we haven't used the document.open ( ) or document.close ( ) methods when to writing to a window. By using only document.write ( ), a new document is opened automatically and subsequent writing is to that document. However, the more formal and appropriate way of writing to windows makes use of the document stream. The document.open ( ) method opens what is called an output stream to collect the output of subsequent document.write ( ) methods. After all such writes are performed, the document.close ( ) method causes any output written to the document stream to be displayed in the document.
<SCRIPT> You can write text, HTML tags, and even pictures to the document stream. The following is a quick example:
<SCRIPT>
It is not necessary to use as many document.write statements as has been done here for readability. Just remember, though, you cannot break a line in the middle of the quotes. Also, when specifying an image file in an <IMG> tag within a document.write statement, you probably will need to use the full URL, beginning with "http://". External DocumentsYou need to keep your web updated with fresh information if it is to be interesting and informative. If your web is part of a business intranet, for example, you might wish to display memos or flyers that are produced routinely, or provide spreadsheet data or charts and graphs that change on a daily basis. One of the ways of providing this information is through secondary windows. By doing so, you do not have to revise your web pages extensively, adding all the HTML code to integrate the changing information. You can simply stick in a button or a link to call up the information in a secondary window.Current Correspondence
<FORM>
<INPUT TYPE=BUTTON VALUE="Chart of 1/1/97 " onclick = "Display ( 'chart1-1-97.html' )" >
<INPUT TYPE=BUTTON VALUE="Graph of 1/1/97 " onclick = "Display ( 'graph1-1-97.gif' )" >
If you use buttons for your secondary window displays, then you can employ a script to control the size and components of the window. In the above example, the Display ( File ) function gets the name of the file to display from the button and formats it in the specially sized and configured window. All items displayed use the same configuration. You can also use an HTML link (<A HREF=...>) with a targeted window (TARGET="Win1"). If the window doesn't exist--which it doesn't--the browser will create one (fully configured) and display the document in it. The above example memo was produced with a word processor (word-wrap off) and then saved as a text file with a .html extension. The only HTML code requirement is to include the <HTML>...</HTML> header and <PRE>...</PRE> format tags surrounding the text. You can, of course, provide full HTML formatting of the document if you want. You can also use text-to-HTML converters or use, say, the Microsoft Internet Assistant add-on for Word to format and save the document in HTML format. The example chart was produced with Microsoft Excel and saved as a text file. A text editor was used to wrap the chart in the <HTML> and <PRE> tags and to save the file with a .html extension. Again, you can use the Microsoft Internet Assistant for Excel to directly produce the HTML file. The example graph was produced within the Excel spreadsheet, copied to the clipboard, and opened as a new document in Corel PhotoPaint. It was then saved as a GIF file for direct import into the web. By using secondary windows you save yourself much of the HTML and JavaScript coding that would be necessary to integrate external documents into the web page. With this technique you only have to add a button or link to make the document accessible. Confirming LinksBefore your scripts proceed to linked documents you may want to get user confirmation. You can tie your links to Confirmation boxes for this verification:
<SCRIPT>
<FORM> Adult Picture! (Netscape Navigator 3.0)
<SCRIPT>
<A HREF="beastiality2.html"
onclick = "return confirm (Msg1+Msg2+Msg3 )"
>Adult Picture!</A> Form ObjectsA form is a property of the document object. It also is an object in its own right. A form object is created when a <FORM> tag appears in the document. The following form containing three elements was created with the accompanying HTML code:
<FORM NAME="MyForm">
If you give a form a name in the <FORM> tag, its elements can be referenced in dot format just as with other JavaScript objects. That is, you can refer to a form element with the notation: document.FormName.ElementName.property So, for instance, the setting for the checkbox--on (true) or off (false)--is accessed and displayed with the notation:
Often, you will need to refer to the values users type in text fields. Here is where you're most likely to get the data that will be processed by your scripts. As you've seen in several examples already, the values residing in text fields are referenced through the value property of the field. In the above example, the value in the field named "MyTextBox" is given by:
As long as the form is named to distinguish it from other forms in your document, you can use this notation even in scripts that refer to elements in more than one form. Every element in every form must have a unique reference--and this can be provided by the form name even if element names are not unique. The following text fields have the same names but are defined in two differently named forms. So, their values can be referenced by the notations shown:
Sometimes when using functions to process form data it is convenient to pass the entire form to the function. You should recognize this method from previous examples:
<SCRIPT>
<FORM>
The parameter this.form in the function call passes a reference to the current form, even though it is not named in the <FORM> tag. This reference is received as argument form in the function. (Note that the name form is nothing special; we could have named the argument anything we wanted.) With a full reference to the form being passed to the function, it makes coding of the function a little easier since we need only to prefix field names with the form. notation rather than with the full document.FormName. notation. At least it takes less typing. Also, with this method, you still can use the same field names in two or more different forms. They are distinguished from one another by the passing of this.form. You can use either method of referring to forms and their fields. It's a matter of preference more than anything else.
|