faqts : Computers : Programming : Languages : JavaScript : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

19 of 24 people (79%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

Can you detect how many browser windows are open?

Jul 17th, 2002 10:37
Lee Grey, Martin Honnen, Greg Miller, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/objects/objects.asp


You can't detect that as there is no browser wide windows 
array/collection.

=========================================

Well, yes and no.  For pure Javascript, that is accurate.

However, it actually is possible to iterate through all open shell 
windows:

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
  function fnGetMyWindows() 
  {		
    var oShell = new ActiveXObject("Shell.Application");
    var oShellWin = oShell.Windows();
    var cnt = oShellWin.Count;
	var result = "<ul>";
	for( i=0; i<cnt; ++i ) {
	  var oWin = new Object;
	  oWin = oShellWin.Item(i);
	  if( oWin != null )
        result += "<li>" + oWin.LocationURL + " - " + oWin.LocationName;
	}					
	result += "</ul>";
    document.all.item("myspot").innerHTML = result;
  }    
-->
</SCRIPT>
</head>
<body onload='fnGetMyWindows()'>
<span id='myspot'>myPath</span>
</body>
</html>

This is problematic in a number of ways.  It only works on IE, 
presumably only in Windows (I don't know anything about IE for the 
Mac), and it makes use of dangerous ActiveX components.  A user who 
lets you do this makes themself vulnerable to anything else you might 
want to do to their machine.  However, it IS possible, and there are 
some pretty good principles to be learned from this small demo, so I 
thought it was worth posting.

Have fun, but play nice!

Lee Grey
Grey Matter
http://www.optioninsight.com