faqts : Computers : Programming : Languages : JavaScript : Document

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

17 of 31 people (55%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How can I automatically close all open layers?

Mar 11th, 2000 01:24
Gee Qman, Nathan Wallace, Martin Honnen


============================================
Source: GeeQman@aol.com

If you're using IE4 or later, you can accomplish your task this way:

1. Name only the layers you wish to manipulate (open/close) at various 
times with a common prefix.  For example, OClyrPic1; OClyrPic2

2. Then you can run a simple script to hide all objects (in this case, 
layers) with that prefix as a wildcard.  For example,

function hideallinfo()
{
	var xx;
	for (xx in document.all)
        {
		if (xx.substr(0,5) == "OClyr") 
			{ 
			eval(xx+'.style.visibility="hidden"')
			}
	}
}

This concept should be used for naming ALL objects on your pages.  Much 
like in Visual Basic, it's a good custom to add a prefix to an object, 
if only to categorize it for later use.  I always use the prefix "lyr" 
for all layered objects on my web pages.  See Microsoft's 
recomendations on this subject here:  
http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbcon98/vbconobjec
tnamingconventions.htm

Good luck!
GQ
GeeQman@aol.com
============================================

Sources: Martin Honnen

With NN4 you have the 

   document.layers

array which you can loop through.

With IE things are different as layers (positioned elements) are not
really distinguished from other elements so you need a way to tell
whether something in

   document.all

is a positioned element.

With IE5 the following might work

function findLayers () {
  var r = new Array();
  var c = 0;
  if (document.all) 
    for (var i = 0; i < document.all.length; i++) {
      var el = document.all[i];
      if (el.currentStyle.position == 'absolute' ||
                el.currentStyle.position == 'relative')
        r[c++] = document.all[i];
  }
  else r = document.layers;
  return r;
}

to find all relatively or absolutely positioned elements whose
visibility you could then change.

Depending on how complex your page is it might be easier to just have a
js array defined by hand which refers to you "layers"