Here's how I thought to do it, but as I don't have a wide knowledge of JavaScript (Only C++) there may be a better way (hence me asking).
Create an object containing the window's handle and a "command on/off" boolean when a window is created, then add the object to an array of the active windows.
When the command is called, eg; /command on, loop through the array of active windows checking the window's handle to the current array object's handle and check if "command on/off" is false, if it is, set it to true.
Same for /command off except changing true, to false.
When the window is destroyed remove which ever object in the array has the same handle.
Another way I thought to do it is using the windows handle as a string in the array but I'm not sure whether JavaScript supports this. I am unsure of the correct term so here's an example:
jscript code:
var fruit = new Array();
fruit['banana'] = "Yellow";
fruit['apple'] = "Red";
fruit['tomatoe'] = "Red"