Java Reference
In-Depth Information
Type something into the text box of the new window. Then return to the original opener window, click
the Get Text button, and you'll see what you just typed into newWindow appear in the text box on the
opener window's form.
Change the text in the opener window's text box and then return to the newWindow and click the Get Text
button. The text you typed into the opener window's text box will appear in newWindow 's t e x t b ox.
Let's look at the opener window fi rst. In the head of the page is a script block in which a variable and
three functions are defi ned. At the top you have declared a new variable, newWindow, which will hold
the window object reference returned by the window.open() method you'll use later. Being outside any
function gives this variable a global scope, so you can access it from any function on the page.
var newWindow;
Then you have the fi rst of the three functions in this page, btnOpenWin_onclick() , which is connected
further down the page to the Open newWindow button's onclick event handler. Its purpose is simply
to open the new window.
Rather than have the new window open up anywhere on the page, you use the built-in screen object,
which is a property of the window object, to fi nd out the resolution of the user's display and place the
window in the middle of the screen. The screen object has a number of read-only properties, but you're
interested here in the width and height properties. You initialize the winTop variable to the vertical
position onscreen at which you want the top edge of the popup window to appear. The winLeft vari-
able is set to the horizontal position onscreen at which you want the left edge of the pop-up window
to appear. In this case, you want the position to be in the middle of the screen both horizontally and
vertically.
function btnOpenWin_onclick()
{
var winTop = (screen.height / 2) - 125;
var winLeft = (screen.width / 2) - 125;
You build up a string for the window features and store it in the windowFeatures variable. You set the
width and height to 250 and then use the winLeft and winTop variables you just populated to create
the initial start positions of the window.
var windowFeatures = “width=250,height=250,”;
windowFeatures = windowFeatures + “left=” + winLeft + “,”;
windowFeatures = windowFeatures + “top=” + winTop;
Finally, you open the new window, making sure you put the return value from window.open() into
global variable newWindow so you can manipulate it later.
newWindow = window.open(“newWindow.htm”,”myWindow”,windowFeatures);
}
The next function is used to obtain the text from the text box on the form in newWindow .
In this function you use an if statement to check two things. First, you check that newWindow is defi ned
and second, that the window is actually open. You check because you don't want to try to access a non-
existent window, for example if no window has been opened or a window has been closed by the user.
The typeof operator returns the type of information held in a variable, for example number, string,
Boolean, object, and undefined. It returns undefined if the variable has never been given a value, as
newWindow won't have been if no new window has been opened.
Search WWH ::




Custom Search