How to intialize muliple windows

I have tried many different variations but can not get this to work correctly when it comes to bringing windows to top by using a dynmic ID name. Please explain how to initialize multiple windows. I do not know the number of windows the user will open so need to give each window an id dynamically. Below are the functions I currently use. if I run the first function it opens a number of times in sequence it opens each window correctly. However if I then run the second function the window opens up blank with no URL attached. Java error: dhxWins.window(“CDiscussWin”+WinID) is null.

function CDiscuss() {
WinPos=WinPos+20;

var WinTop=posTop()+(80+WinPos);
var WinRight=posRight()-(860+WinPos);

if (!dhxWins) dhxWins=new dhtmlXWindows();
dhxWins.createWindow(“CDiscussWin”+WinID,WinRight,WinTop,823,255);
dhxWins.window(“CDiscussWin”+WinID).setText(“New Discussion”);
dhxWins.window(“CDiscussWin”+WinID).attachURL(“CDiscussWindow.php?NR=”+row+“&S=”+customer+“&C=”+Contact+“&E=”+CExternal+“&SoftDB=”+SoftBase+“&ClientID=”+ExtID+“&Priv=P&P=”+P+“&WinID=”+WinID+“&”);
dhxWins.window(“CDiscussWin”+WinID).denyResize;
dhxWins.attachEvent(“onFocus”, function() {
dhxWins.window(“CDiscussWin”+WinID).bringToTop();
});
WinID++;
}

function SDiscuss() {
WinPos=WinPos+20;
alert("WinID is "+WinID)

var WinTop=posTop()+(80+WinPos);
var WinRight=posRight()-(860+WinPos);

if (!dhxWins) dhxWins=new dhtmlXWindows();
dhxWins.createWindow(“SDiscussWin”+WinID,WinRight,WinTop,823,255);
dhxWins.window(“SDiscussWin”+WinID).setText(“New Discussion”);
dhxWins.window(“SDiscussWin”+WinID).attachURL(“SDiscussWindow.php?NR=”+row+“&S=”+supplier+“&C=”+Contact+“&E=”+CExternal+“&SoftDB=”+SoftBase+“&SupplierID=”+ExtID+“&Priv=P&P=”+P+“&WinID=”+WinID+“&”);
dhxWins.window(“SDiscussWin”+WinID).denyResize;
dhxWins.attachEvent(“onFocus”, function() {
dhxWins.window(“SDiscussWin”+WinID).bringToTop();
});
WinID++;

}

I’ve checked your issue:

  1. you use one event handler on 2 ids at the same time
  2. handler ‘onFocus’ passes as parameter window’s ID, and you can use it
  3. also you may set a check for event ‘onFocus’

And found a solution:

Declare 2 global variables (f.e. dhxWins & focusEvent) and try to replace in both functions the next pieces:

your:

dhxWins.attachEvent("onFocus", function() { dhxWins.window("CDiscussWin"+WinID).bringToTop(); });

to my:

if (!focusEvent){ focusEvent = dhxWins.attachEvent("onFocus", function(win) { win.bringToTop(); }); }

OK so the attachEvent is global for all windows. So what I need is a seperate attachEvent for each window. I tested this by creating a seperate variable for each window and that works fine.
The problem however is how do you create a dynamically named variable for each window:

win1.attachEvent …
win2.attachEvent …
etc

So need:
var win1=dhxWins.createWindow(…
var win2=dhxWins.createWindow(…

Where win1 and win2 variables are creating dynamically each time a new window is opened by incrementng the numeric portion of the variable. How can I create this dynamically named variable?

OK I have figured this out. Quite simply really.

var win=“w”+WinID
win=dhxWins.createWindow(…

You don’t need to apply the 'onFocus" event to each window. This event gets an ID as parameter. And if you make this event Global - you will have no difficulty.

See the attached sample
creating_windows.rar (46.5 KB)