COM271, Week 9

The Window Object: Opening, closing, and specifying windows

Syllabus | Table of Pages | Assignments | References and Useful Links

The DOM Window Object includes methods to create (open), destroy (close), and to set an array of attributes of new windows. If you want to allow access to a new page without forcing a visitor to leave your site, you may do this by opening a new window for the new page. The new window should include, along with the content, a means to close / destroy the window. You may also set up scroll bars, determine the size and position of the new window, etc.

The general syntax is
window.open(URL,name,specifications,replace)
where the arguments are as follows (from W3Schools Window open() Method):

ParameterDescription
URLOptional. Specifies the URL of the page to open. If no URL is specified, a new window with about:blank is opened
name Optional. Specifies the target attribute or the name of the window. The following values are supported:
  • _blank - URL is loaded into a new window. This is default
  • _parent - URL is loaded into the parent frame
  • _self - URL replaces the current page
  • _top - URL replaces any framesets that may be loaded
  • name - The name of the window
specsOptional. A comma-separated list of items. The following values are supported:

channelmode=yes|no|1|0Whether or not to display the window in theater mode. Default is no. IE only
directories=yes|no|1|0Whether or not to add directory buttons. Default is yes. IE only
fullscreen=yes|no|1|0Whether or not to display the browser in full-screen mode. Default is no. A window in full-screen mode must also be in theater mode. IE only
height=pixelsThe height of the window. Min. value is 100
left=pixelsThe left position of the window
location=yes|no|1|0Whether or not to display the address field. Default is yes
menubar=yes|no|1|0Whether or not to display the menu bar. Default is yes
resizable=yes|no|1|0Whether or not the window is resizable. Default is yes
scrollbars=yes|no|1|0Whether or not to display scroll bars. Default is yes
status=yes|no|1|0Whether or not to add a status bar. Default is yes
titlebar=yes|no|1|0Whether or not to display the title bar. Ignored unless the calling application is an HTML Application or a trusted dialog box. Default is yes
toolbar=yes|no|1|0Whether or not to display the browser toolbar. Default is yes
top=pixelsThe top position of the window. IE only
width=pixelsThe width of the window. Min. value is 100
replaceOptional. Specifies whether the URL creates a new entry or replaces the current entry in the history list. The following values are supported:
  • true - URL replaces the current document in the history list
  • false - URL creates a new entry in the history list

Note that if a user's browswer has a pop-up killer installed, opening a window may not work.

Open a Window!

A simple window: Here is a link with an event handler to open a new window that will include the Google.com home page: Google this!

Here is the code:
<a onclick="newWindow = window.open('http://www.google.com', 'google','outerHeight=600, outerWidth=800,scrollbars=yes');" href="#">Google this!</a>

No! Close that Window!

Notice that newWindow is the name of an instance of a window object. You may apply the close method if you are finished using the window
<a onclick="newWindow.close>Click to close window</a>
and it's gone!

JS & DOM Reference

Overview

JavaScript Objects

JS Array JS Boolean JS Date JS Math JS Number JS String JS RegExp JS Global

Browser Objects

Window Navigator Screen History Location

HTML DOM Objects

Document Events Elements Anchor Area Base Body Button Form Frame/IFrame Frameset Image Input Button Input Checkbox Input File Input Hidden Input Password Input Radio Input Reset Input Submit Input Text Link Meta Object Option Select Style Table TableCell TableRow Textarea