1. Technology

The Browser Object Model

7. Manipulating Windows


The Browser Object Model contains a number of methods that are intended to interact with the browser window itself. Now that most browsers support the use of tabs and give more control to the browser owner as to where and how pages are opened these methods are far less useful than they used to be. They are however the only way that you have any say at all in what happens with your visitor's browser.

The window.open() method opens a new browser window or rather that is its default action if your visitor hasn't set their browser up to have it open a new tab in their existing browser instead. This method takes up to four parameters although the fourth is seldom used. The first parameter identifies the page that is to bbe opened in the new window or tab while the second parameter specifies the name of that window. If a window by that name already exists then that window or tab will be used rather than opening a new one.

The third parameter contains suggestions on how the new window should look (if the browser does actually open a new window - it is ignored if the command opens a new tab). Here you can suggest the size the new window should have, where it should appear on the screen and what toolbars it should include. These are only suggestions though since most browsers give their owner the option to decide which of these that can be applied (and in some instances completely ignore those entries such as removing the address and status bar which have been determined to cause security issues).

The other BOM methods which were created to allow an existing window to be adjusted do not usually work in modern browsers at all. They are guaranteed not to work where tabs are being used and will probably be disabled in most other instances. The window.moveBy(), window.moveTo(), window.resizeBy(), and window.resizeTo() are the only way that is provided that allows you to suggest moving or resizing the browser window once it is open.

That brings us to closing a window or tab which can be done using the close() method. This does have one major security issue that you need to understand regarding when it can work and that is that the window or tab you are closing must have first been opened by JavaScript running on your site in order for your site to be able to close it. If you didn't open the window or tab then you can't close it. Attempting to close a window or tab you didn't open will be completely ignored by most browsers and the best you can hope for with browsers that do not ignore it is that it will pop up an alert telling your visitor that you are trying to close their browser so that they can cancel it.

More of this Tutorial

  1. About.com
  2. Technology
  3. JavaScript

©2014 About.com. All rights reserved.