Weird Thoughts From Eric's Head

Categories : All | AJAX | BUSINESS | PERSONAL | PROGRAMMING | BOOK REVIEW

Poor Heated Marriage
A poor man and woman sat down in their living room and the man said, ''I'm going down to the pub for a bit, so put your coat on.''

The woman replied, ''Oh, sweetie, why? Are you taking me with you?''

The man replied, ''No, I'm turning the heat off.''

Closing Multiple Pop Up Windows
A common question I see on many forums is, I have multiple pop up windows and I want them to all be closed with a click of a button. This can be rather easy if you make sure you open your pop up windows in a wise manner.

The first thing I am assuming is you know how to make a pop up window so I am not going in detail with that. When you create a pop up window you need to set it to a variable to store the object. The reason for this is so you can reference the window. Yesterday I showed how to send data back and forth with this reference. With this reference you can use the close() method to close the child window.

For example the following code opens a window and closes it after a set period of time.

winPop = window.open("about:blank");
var timer = setTimeout("winPop.close()",1000) 
If you open up windows with the same object reference (e.g. winPop) it gets overwritten each time so it makes it basically impossible to reference the window from the parent. Therefore we need to attack it from another path. Using an array will make it very easy. Each time you open up a new window you increase the index. To close all of the windows all you need to do is to loop through the array.

Now when you loop through the array you need to make sure that it was created and that it is not closed. You do that with winPop && !winPop.closed. Since we are using an array, all we need to do is add [i] to winPop so we can reference each of the indexes.

Below is an example that opens up three windows and closes them after a set period of time:
  var winPop = new Array();
  winPop[winPop.length] = window.open("about:blank");
  winPop[winPop.length] = window.open("about:blank");
  winPop[winPop.length] = window.open("about:blank");

  function closePops(){
    for(i=0;i<winPop.length;i++){
      if(winPop[i] && !winPop[i].closed){
        winPop[i].close()
      }
    }
  }

  var timer = setTimeout("closePops()",1000);
Hope this helps you out.

Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages




Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 9 + 1 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1106152877000