|
|
The JavaScript Pop-up Window Primer
You are here: irt.org | Articles | JavaScript | Window | The JavaScript Pop-up Window Primer Published on: Monday 9th November 1998 By: Martin Webb
Introduction to JavaScript Pop-up WindowsFor most people, the main browser window is the only one they ever use or need. However, it is possible for another window to be opened up to allow you to either view another page while retaining the existing page in the main window, or to open up a remote window that can control or be controlled by the main browser window. Another browser window can be simply opened using HTML and the TARGET attribute, either in a link or on a form:
or:
As long as the TARGET attribute specifies a window name that is not already open, a new window will be created mimicing the existing window. If the window already exists, then the contents will be changed instead. The problem with this approach is that apart from the window name there is nothing in common between the two windows. You can change the location of the new window using a similar link for form target - but that's it. Whereas using JavaScript and the window.open() method, it is possible to control the look and feel of the new window, to have control over its contents, and also to be controlled by the new window. Window open syntaxThe syntax of the open() method is fairly straight forward:
In addition to this, MSIE4 has a fourth parameter:
windowHandle
URL
If no URL is specified, a new window with about:blank will be displayed.
windowName
Features
If you don't specify the width or height values, then Netscape browsers will mimic the size of the current window, if you don't specify any attributes then Netscape will mimic all the features of the current window:
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:
Replace Stringing it togetherSo 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:
Managing Your WindowsMaintaining their focus and keeping them centeredYou 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:
Keeping windows centeredIn NN4, the screenX and screenY attributes were introduced. In MSIE4 the top and left attributes were introduced. By combining the two both NN4 and MSIE4 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 NN4 and the screen objects width and height properties in MSIE4. However, this results in the pop-up window being centered within the confines of the main browser window in NN4, and centered within the confines of the screen in MSIE4 - not the same thing, unless the main browser window is maximized.
New Window FeaturesBoth NN4 and MSIE4 introduced new window features that can be set when opening up a new window:
In NN4, to create a window smaller than 100 pixels high by 100 pixels wide then signed scripts will be required, other features like setting a window as always lowered or offscreen also require signed scripts - this is to avoid a window that the user cannot see. To find out more about using signed scripts in NN4 see Chapter 7, JavaScript Security, in Netscape's JavaScript Guide. Controlling Pop-up WindowsThe 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. Changing LocationThe 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:
Writing things downThe 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.
InterrogationWith 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:
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 similarily, to perform the reverse alert(window.one.location.href) will highlight the location of the left frame from the parent frame, and alert(parent.two.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, e.g., 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. Unfortunately, the opener property was not made available in older browsers - it was introduced in NN3 - but, we are able to create our own window properties/variables at any time:
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. Date pop-up windowTo round of this article, I've included a practical example of using pop-up windows. The form button, when pressed, opens up a date selector. Once a date has been selected the date is returned to the form on this page.
The date selector is described in detail in the article Popup Date Selector This article has attempted to show you how to create new windows with properties and attributes that you can specify yourself. We have also seen how to write to these windows and how to access one window from another. Hopefully this will encourage you to make more use of the windows open() method. Feedback on 'The JavaScript Pop-up Window Primer'
View the profile on Martin Webb and the list of other Articles by Martin Webb. |
-- div -->
|