1. Home
  2. Computing & Technology
  3. JavaScript

Popup Windows

6. Reusing the Popup Window

clr gif

There may be times when you want to place multiple links on your site that are all intended to open a popup window but displaying different content in that popup window. Rather than annoy your visitors more than necessary by creating separate popup windows for each of these (and potentially ending up with lots of windows on their screen) we can reuse the existing popup window when they click on a second (and subsequent links) that are set to open a popup window.

As you will remember from the first two tutorials we can either create a popup that will load a separately defined page (like this):

window.open('mywin.htm','name','height=255,width=250,
continued from previous line toolbar=no,directories=no,status=no,menubar=no,
continued from previous line scrollbars=no,resizable=no');

or alternatively we can generate the page content from Javascript (in which case the open statement looks like this):

TheNewWin = window.open('','name','height=255,width=250,
continued from previous line toolbar=no,directories=no,status=no,menubar=no,
continued from previous line scrollbars=no,resizable=no');

In each of these you will notice that the second parameter is shown in bold. This parameter is the one that gives the new window a name. If an open statement is run that is supposed to open a window where there is already a window open that has that name then instead of opening a new window the existing window will be used.

You don't have to call your popup window name as I have in the above examples, you can call it whatever you want. If you have two or more links that open windows where you have supplied the same name then the existing popup window opened by the first link will be used if it is still open when they select the second link. If this happens then all of the subsequent parameters regarding the size of the window and which toolbars etc. to be displayed will be ignored and the existing size and setup will be used.

Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.