Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing

Example 13.5:
Using the Object Array to View Properties


This form displays the property names and values for an object. Select an object using the radio buttons and click on the display button:
Caution: This program crashes some browsers, due to a JavaScript bug in some versions. The Atlas Preview Release 2, for instance, will crash if you use the Window option button.
Text Box window document form Math Button Radio History
Textbox object:


We have these functions in the HEAD: <SCRIPT LANGUAGE="JavaScript"> <!-- //====================================================================== //Set up the newline characters to be used in the textarea box - this depends on which platform //the browser is running on var nl=null if (navigator.appVersion.lastIndexOf('Win') != -1) { nl = "\r\n" } else { if (navigator.appVersion.lastIndexOf('Mac') != -1) { nl = "\r" } else { nl = "\n" } } //========================================================================== function show_properties(form, obj, obj_name) { var sDisplay = "" var i = 0 for(i in obj){ sDisplay = sDisplay + obj_name + "." + i + " = " + obj[i] + nl } form.txtObjectList.value = sDisplay } //========================================================================== function ClearAll(form) { //Clear the textarea box: form.txtObjectList.value = "" } //====================================================================== //Work out which object to display: function ObjectType(form) { //Clear the textarea box: form.txtObjectList.value = "" if (form.radObjectType[0].checked) { show_properties(form, form.TextProps, "Textprops") return } if (form.radObjectType[1].checked) { show_properties(form, window, "window") return } if (form.radObjectType[2].checked) { show_properties(form, document, "document") return } if (form.radObjectType[3].checked) { show_properties(form, form, "Form1") return } if (form.radObjectType[4].checked) { show_properties(form, Math, "Math") return } if (form.radObjectType[5].checked) { show_properties(form, form.butDisplay, 'butDisplay') return } if (form.radObjectType[6].checked) { show_properties(form, form.radObjectType, 'radObjectType') return } if (form.radObjectType[7].checked) { show_properties(form, history, 'history') return } } //========================================================================== //--> </SCRIPT> Then we have this form: <FORM NAME="form1"> <INPUT TYPE="radio" NAME="radObjectType" CHECKED VALUE = "TEXT" onclick="ClearAll(this.form);">Text Box <INPUT TYPE="radio" NAME="radObjectType" onclick="ClearAll(this.form);">window <INPUT TYPE="radio" NAME="radObjectType" onclick="ClearAll(this.form);">document <INPUT TYPE="radio" NAME="radObjectType" onclick="ClearAll(this.form);">form <INPUT TYPE="radio" NAME="radObjectType" onclick="ClearAll(this.form);">Math <INPUT TYPE="radio" NAME="radObjectType" onclick="ClearAll(this.form);">Button <INPUT TYPE="radio" NAME="radObjectType" onclick="ClearAll(this.form);">Radio <INPUT TYPE="radio" NAME="radObjectType" onclick="ClearAll(this.form);">History <BR> Textbox object: <INPUT TYPE="text" NAME="TextProps" size=50 VALUE="Contents of text box - try changing this")> <BR> <INPUT TYPE="button" VALUE="Display the Object's Properties" NAME=butDisplay onclick="ObjectType(this.form)"> <BR> <TEXTAREA NAME="txtObjectList" ROWS=10 COLS=73></TEXTAREA> </form>
Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing