The Little Guy Posted February 8, 2008 Share Posted February 8, 2008 I am trying to get this function to work: function msgUsr(userID){ if(eval("!user_"+userID)){ eval("var user_"+userID+" = window.open('/MCIM/msgbuddy.php?userID='+userID,'','scrollbars=no,menubar=no,height=300,width=400,resizable=yes,toolbar=no,location=no,status=no');"); } } It is supposed to check to see if a window is already open. the window var is created dynamically, the problem I am having, is the if statement, if when it gets there it says it can not find user_2 or user_3 and so on so what is wrong with that? If the window is not found, it should go into the if statement, if the window is found, it should ignore the new window open. Quote Link to comment Share on other sites More sharing options...
nogray Posted February 8, 2008 Share Posted February 8, 2008 Try to avoid eval (search for "eval is evil") You can do the same with using a simple JS object <script language="javascript"> var wins = {}; function open_win(user_ID){ if (!wins[user_ID]){ wins[user_ID] = window.open("http://www.google.com", "", ""); } } </script> <input type="button" onclick="open_win(2);" value="Open" /> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 8, 2008 Author Share Posted February 8, 2008 Works Perfect! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 8, 2008 Author Share Posted February 8, 2008 I did find one problem though, If you open a window, then close the window, you can not reopen the window unless the page is is refreshed... so, when a window is closed, how do you destroy the window value? Quote Link to comment Share on other sites More sharing options...
nogray Posted February 8, 2008 Share Posted February 8, 2008 add this function to your main window function removeWin(user_ID){ delete wins[user_ID]; } and on the opended window, add an onunload event <body onunload="opener.removeWin(user_ID);"... Just make sure to use the same user_ID Not tested. Both pages has to be in the same domain Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.