
Note all the name-value pairs must be separated by commas, but must not have spaces between them. For example, the following is incorrect:
Whereas the following, which does not have spaces, is correct:
The values for the Boolean parameters may be 0 or 1 or yes or no. If a particular Boolean parameter is not specified then its value defaults to no. For example, to open a window without directory buttons, location field, menubar, scrollbars, status or toolbar and not resizable use:
Again, if you don't specify the width and height then the page mimics the current window. However, to create a window with all the window features you need to explicitly include them:
So now that we know how to create a window with any features we desire, how can we use JavaScript to control the features we want? The first three parameters are all strings; JavaScript can manipulate strings just as well as any other programming language.
Which you can try out for yourself:
You may have noticed that its quite easy to lose pop-up windows - they can become hidden behind the main browser window. This happens when the main browser window regains focus, i.e., becomes the selected window. To avoid this you need to decide whether you want the pop-up window to have exclusive control of the browser, or whether you want it to be closed, or even brought to the front after a short delay. By placing the following in the document loaded into the pop-up window then the pop-up will remain in front of the main browser window:
This has the side effect on some browsers of inhibiting the use of the main browser window until the pop-up window is closed. If you don't require this feature then you could always refocus the pop-up window after a delay:
Which ensures that the pop-up window regains the focus after a delay of one second - enough time to allow the user to interact with the main browser window. Of course you may decide that if the user has moved the focus to the main browser window, that the pop-up window should be closed:
In Netscape 4.x, the screenX and screenY attributes were introduced. In IE 4.x the top and left attributes were introduced. By combining the two both Netscape 4.x and IE 4.x will allow you to position the window. In earlier browsers the attributes and their values will be safely ignored:
To actually center the pop-up window requires the use of the window objects outerWidth and outerHeight properties in Netscape 4.x and the screen objects width and height properties in IE 4.x. However, this results in the pop-up window being centered within the confines of the main browser window in Netscape 4.x, and centered within the confines of the screen in IE 4.x - not the same thing, unless the main browser window is maximized.
The windowHandle allows you to control the contents of the pop-up window; either to change the location of the pop-up window, to write HTML code into the window, or to interrogate the window properties. The following shows how to alter the location of the pop-up window from blank.htm to testpage.htm:
There are potential problems with the code above. There are occasions where the window may not actually be opened before the change of location is attempted. It is best to change the above code to introduce a slight delay to give the browser a chance to actually open the window first. The following introduces a one second delay before the location is changed:
The following demonstrates how to write directly to the pop-up window using the windowHandle in conjunction with the documents write method. The first occasion that the window is written to will cause the existing contents to be replaced, other following document writes append information to the window.
With frames in a frameset its fairly straight forward to access the parent frame or the child frame, all you need to do is remember the object hierarchy. For example, the following frameset:
Produces the following structure: Parent Frame
|
+----+----+
| |
one.htm two.htm
To access the parent frame from one.htm, use simply use: alert(parent.location.href), which will highlight the location of the parent frame, and similarly, to perform the reverse alert(left.location.href) will highlight the location of the left frame from the parent frame, and alert(parent.right.location.href) will highlight the location of the right frame from the left frame. Once you've established this simple object syntax, you can use it to read and write almost any property of any other window within the frameset. However, when you've opened a new window, the window does not form part of a frameset - therefore a new means of accessing the new window from the opener (i.e., the window that opens the new window) is required and vice versa. Well, accessing the new window is straight forward enough, you just use the window name as shown in the previous example. Modern browsers also include a means of accessing the opener window. Funny enough, by providing an opener property, ie, window.opener, which will reference the window, if any, that opened the current window. For example alert(window.opener.location.href) will highlight the location of the current window's opener.
Which checks to see whether the new window already has an opener property, and if not, creates one with a reference to the current window. This new window property can be used as if the browser supplied the opener property itself. To round this off, I've included a practical example of using pop-up windows. The Form button, when pressed, opens up a Calendar with appropriate Month and Year. <SCRIPT>function getCal(form){ open('calendar.htm?Month='
+ month + '&' + 'Year=' + year, 'myExample11', </SCRIPT> <FORM> Hopefully, this showed you how to create new windows with properties and attributes that you can specify yourself. You've also seen how to write to these windows and how to access one window from another. |