Re: how to hide every <div> ?
On Thu, 12 Aug 2004 09:30:52 +0100, john henry bonham <with@he.ld> wrote:
[color=blue]
> Richard Cornford wrote:
>[color=green]
>> john henry bonham wrote:
>> <snip>
>>[color=darkred]
>>> for (var i = 0 ;i < document.getEle mentsByTagName{ 'div' ).length;
>>> i++ ); {
>>> document.getEle mentsByTagName{ 'div' )[ i ].style.visibili ty =
>>> 'hidden';
>>> }[/color]
>> <snip>
>> Apart form the absence of verification for the browser's supportfor the
>> required features (which can be assumed to be in codepreviously
>> executed but not shown), This is a ridiculouslyine fficient formulation.[/color]
>
> Sorry you lost me. Can you explain that in layman's terms?[/color]
Just what he said.
Every time you call document.getEle mentsByTagName( ), a supporting browser
must search the document for all such tags and build an array containing
them. To make matters worse, it must do this twice for each iteration:
once to get the length property, and again to get a reference to the i'th
object.
Note that I said "supporting browser", above. As you point out yourself,
this method is only supported by relatively recent browsers. There are
many people that use browsers that will not support this method, so this
script will do nothing more than cause an error.
To sort out the latter problem, use feature testing:
if(document.get ElementsByTagNa me) {
// method supported
This should also be applied when accessing the style property.
For the former, call the method once, save the results, and use them in
the loop:
function setVisibility(e , v) {
if(e) {
if(e.style) {
e.style.visibil ity = v;
} else {
e.visibility = v;
}
}
}
function hideDivs() {
if(document.get ElementsByTagNa me) {
var c = document.getEle mentsByTagName( 'DIV');
for(var i = 0, n = c.length; i < n; ++i) {
var e = c[i];
setVisibility(e , 'hidden');
}
}
}
[color=blue]
> Also, you have spent alot of effort in this thread pointing outerrors
> and standars which is all well and good. What's yoursolution to the
> problem? Or pray, tell us which solution offered isthe best.[/color]
Well, the above could be modified to use alternatives to
getElementsByTa gName():
if(document.get ElementsByTagNa me) {
var getRefsByName = function(n) {
return document.getEle mentsByTagName( n);
};
} else if(document.all && document.all.ta gs) {
var getRefsByName = function(n) {
return document.all.ta gs(n) || [];
};
/*
* Add any other alternatives here.
*
*/
} else {
var getRefsByName = function() {
return [];
};
}
function setVisibility(e , v) {
if(e) {
if(e.style) {
e.style.visibil ity = v;
} else {
e.visibility = v;
}
}
}
function hideDivs() {
var c = getRefsByName(' DIV');
if(c) {
for(var i = 0, n = c.length; i < n; ++i) {
var e = c[i];
setVisibility(e , 'hidden');
}
}
}
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
On Thu, 12 Aug 2004 09:30:52 +0100, john henry bonham <with@he.ld> wrote:
[color=blue]
> Richard Cornford wrote:
>[color=green]
>> john henry bonham wrote:
>> <snip>
>>[color=darkred]
>>> for (var i = 0 ;i < document.getEle mentsByTagName{ 'div' ).length;
>>> i++ ); {
>>> document.getEle mentsByTagName{ 'div' )[ i ].style.visibili ty =
>>> 'hidden';
>>> }[/color]
>> <snip>
>> Apart form the absence of verification for the browser's supportfor the
>> required features (which can be assumed to be in codepreviously
>> executed but not shown), This is a ridiculouslyine fficient formulation.[/color]
>
> Sorry you lost me. Can you explain that in layman's terms?[/color]
Just what he said.
Every time you call document.getEle mentsByTagName( ), a supporting browser
must search the document for all such tags and build an array containing
them. To make matters worse, it must do this twice for each iteration:
once to get the length property, and again to get a reference to the i'th
object.
Note that I said "supporting browser", above. As you point out yourself,
this method is only supported by relatively recent browsers. There are
many people that use browsers that will not support this method, so this
script will do nothing more than cause an error.
To sort out the latter problem, use feature testing:
if(document.get ElementsByTagNa me) {
// method supported
This should also be applied when accessing the style property.
For the former, call the method once, save the results, and use them in
the loop:
function setVisibility(e , v) {
if(e) {
if(e.style) {
e.style.visibil ity = v;
} else {
e.visibility = v;
}
}
}
function hideDivs() {
if(document.get ElementsByTagNa me) {
var c = document.getEle mentsByTagName( 'DIV');
for(var i = 0, n = c.length; i < n; ++i) {
var e = c[i];
setVisibility(e , 'hidden');
}
}
}
[color=blue]
> Also, you have spent alot of effort in this thread pointing outerrors
> and standars which is all well and good. What's yoursolution to the
> problem? Or pray, tell us which solution offered isthe best.[/color]
Well, the above could be modified to use alternatives to
getElementsByTa gName():
if(document.get ElementsByTagNa me) {
var getRefsByName = function(n) {
return document.getEle mentsByTagName( n);
};
} else if(document.all && document.all.ta gs) {
var getRefsByName = function(n) {
return document.all.ta gs(n) || [];
};
/*
* Add any other alternatives here.
*
*/
} else {
var getRefsByName = function() {
return [];
};
}
function setVisibility(e , v) {
if(e) {
if(e.style) {
e.style.visibil ity = v;
} else {
e.visibility = v;
}
}
}
function hideDivs() {
var c = getRefsByName(' DIV');
if(c) {
for(var i = 0, n = c.length; i < n; ++i) {
var e = c[i];
setVisibility(e , 'hidden');
}
}
}
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Comment