Window Properties
Here is the source code for a web page
containing JavaScript that:
- opens child windows as the main (parent) window loads
- This could have been done in response to the onLoad event, but here it's done another
way.
- modifies properties of the children
- closes its child windows with the window.close()
method...
- ...when parent closes or goes to a new page (onUnload()).
- A child window refers to its parent as window.opener.
The Window
object is the top-level object in Javascript.
- One such object can create others.
- They can modify each other's properties and sub-objects.
- The document
sub-object is particularly interesting. Its properties and methods include:
Caution: Netscape's handling of child windows is buggy.
For example, in some versions, Netscape crashes if you attempt to change a property of
a non-existent child window.
Lab
Your task:
- Make a copy of the example web page and modify it as follows.
- Add a button to the form.
- Reverse the given example so that the child window changes the parent's
properties:
- Change the parent window's background color (its bgcolor property).
- That is, assign a new value to window.opener.document.bgcolor.
- Change the parent window's button label (its form.field.value property,
where form is the name of the form in the parent document, and field
is the name of the button).
- That is, assign a new value to window.opener.document.form.field.value.
- Add a handler for the onUnload() event that closes the parent window
when the child closes.
Solution
Don't peek unless you're stuck!
Extra for Experts
- The child windows opened by our example are not resizable. Fix this.
- Add a text field to the form in the parent window.
- From the child, generate some text in your new text field in the parent window.
- Do this by modifying the value property of the text field object.
- The generated text should be the name of the winery selected in the child window.