469,268 Members | 942 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

GetElementByTagName, document.All & document.Layers - What's up with that?

Hi,
Thanks for any help in advance... Okay, I have the JS listed
below that calls for the display of the (DIV) tag... cookie
function not included, as don't feel its necessary but you'll
get the idea!
function closeall()
{
var objs;

if (document.all)
{ objs = document.all.tags("DIV");
alert('1all')
}
else if (document.getElementsByTagName)
{ objs = document.getElementsByTagName("DIV");
alert('1getElement')
}
else if (document.layers)
{ objs = document.layers["DIV"];
alert('1Layers')
}
else
{ alert('Sorry, Your browser does not support our Main
Navigation system'); }

for (var i=0; i<objs.length; i++) {
if (objs[i].className = "small")
{
objs[i].style.display = "none";
}
}
}

function expandDiv(tahw) {
setCookie("thingsmenu", tahw, 7);
closeall();

what = tahw + "_menu"

if (document.all(what).style.display = "none") {
document.all(what).style.display = "";
alert('2all')
}
else if (document.getElementsByTagName(what).style.display =
"none") {
document.getElementsByTagName(what).style.display = "";
alert('2getElement')
}
else { document.layers(what).style.display = "none";
alert('2layers')
}

}

function setmenu()
{
var menuvar;
menuvar = getCookie("thingsmenu");

expandDiv(menuvar);
}
This works both in IE and Firefox but not Netscape...
The results from the alerts (placed for debug) are as follows

IE: 1all - 2all
FireFox: 1getElement - 2all
NS: 1getElement - then returns nothing!!

Not convinced that Firefox is right - why 1getElement the 2all?
Anyhow it works.. just need to get Netscape working, any ideas?

Appreciate it

----------------------------------------------
Posted with NewsLeecher v2.0 Beta 5
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------

Jul 23 '05 #1
10 54557


InvisibleMan wrote:

else if (document.layers)
{ objs = document.layers["DIV"];
Netscape 4 doesn't allow you to select "layer" elements by tagname so
that approach is likely to fail.

This works both in IE and Firefox but not Netscape...
Which Netscape version exactly? What does the Netscape JavaScript
console say, any error messages?

The results from the alerts (placed for debug) are as follows

IE: 1all - 2all
FireFox: 1getElement - 2all
NS: 1getElement - then returns nothing!!

Not convinced that Firefox is right - why 1getElement the 2all?
Firefox (at least 1.0 and later) as well as Mozilla 1.7.5 and later in
quirks mode supports unchecked access to document.all thus the

if (document.all(what).style.display = "none") {
document.all(what).style.display = "";
alert('2all')
}

will execute.
Anyhow it works.. just need to get Netscape working, any ideas?


Netscape 7.2 is based on Mozilla 1.7.2 and there the document.all
support is not there and thus the expression
document.all(what).style.display
will give a script error.
So you would need to properly check
if (document.all) {
// use document.all here
}
else if (document.getElementsByTagName) {
// use document.getElementsByTagName
}
or whatever collection or method you want to use.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
InvisibleMan wrote:
Hi,
Thanks for any help in advance... Okay, I have the JS listed
below that calls for the display of the (DIV) tag... cookie
function not included, as don't feel its necessary but you'll
get the idea!
function closeall()
{
var objs;

if (document.all)
{ objs = document.all.tags("DIV");
alert('1all')
}
else if (document.getElementsByTagName)
{ objs = document.getElementsByTagName("DIV");
alert('1getElement')
I would swap these around and do document.getElementsByTagName
first. That will include most of IE and just about everything
else. document.all will pick up older IE.

I'm not near IE right now, but I also think IE prefers square
brackets for document.all, not round:

{ objs = document.all.tags["DIV"];
}
else if (document.layers)
{ objs = document.layers["DIV"];
alert('1Layers')
}
else
{ alert('Sorry, Your browser does not support our Main
Navigation system'); }
This message is also backward - your 'Main Navigation system'
does not support the users browser. It would be helpful if you
told them which browsers you do support and offered links on
where to get them from.

for (var i=0; i<objs.length; i++) {
if (objs[i].className = "small")
This will set objs[i].className to "small", so the test will
always succeed and all divs will have their classname set to
"small" and display set to 'none'. Did you meant to write:

if (objs[i].className == "small")
{
objs[i].style.display = "none";
}
}
}

function expandDiv(tahw) {
setCookie("thingsmenu", tahw, 7);
closeall();

what = tahw + "_menu"

if (document.all(what).style.display = "none") {
Here again, you are not using a comparison but an assignment.
Did you mean:

if (document.all[what].style.display == "none") {
document.all(what).style.display = "";
alert('2all')
}
else if (document.getElementsByTagName(what).style.display =
"none") {
and again...
document.getElementsByTagName(what).style.display = "";
alert('2getElement')
}
else { document.layers(what).style.display = "none";
alert('2layers')
}

}

function setmenu()
{
var menuvar;
menuvar = getCookie("thingsmenu");
This could be:

var menuvar = getCookie("thingsmenu");

expandDiv(menuvar);
}
This works both in IE and Firefox but not Netscape...
The results from the alerts (placed for debug) are as follows

IE: 1all - 2all
FireFox: 1getElement - 2all
NS: 1getElement - then returns nothing!!

Not convinced that Firefox is right - why 1getElement the 2all?
Because your "test" was not a test but an assignment that will
always be true and not get to "2getElement"
Anyhow it works..


You think it works - it doesn't do what you think it's doing.
--
Fred
Jul 23 '05 #3
I'm using NS7.2 - I know that the script did work on NS6...
Really want this to work on most Browser version (or as many as
possible considering the variations)

is NS4 getElemenyByID - i think that rings bells some how...

Just reading the rest of your reply and not sure quite how to
impliment... brain is fried with this to ages just to get it where
it is...

Appreciate the info

----------------------------------------------
Posted with NewsLeecher v2.0 Beta 5
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------

Jul 23 '05 #4
InvisibleMan wrote:
I'm using NS7.2 - I know that the script did work on NS6...
Really want this to work on most Browser version (or as many as
possible considering the variations)

is NS4 getElemenyByID - i think that rings bells some how...

Just reading the rest of your reply and not sure quite how to
impliment... brain is fried with this to ages just to get it where
it is...
Each fix is written immediately below the line it applies to,
except for the last one:
else if (document.getElementsByTagName(what).style.display =

"none") {

becomes:

else if (document.getElementsByTagName(what).style.display ==
"none") {
Just replace them line for line. Sorry if line wrapping has
affected the above, they should be one line.

For the record:

"=" is an assignment, it means evaluate the stuff on the right
and put the result into the thing on the left.

"==" is a comparison - evaluate both sides, and if the results
are the same, then "true" results. Otherwise, it's false.


--
Fred
Jul 23 '05 #5
Okay Netscape 7.2 is still jigged! - Javascript console says:

Error: document.all is not a function

if I make do:

if (document.all(what).style.display = "none") {
document.all(what).style.display = "";
alert('2all');
}
else if (document.getElementsByTagName(what).style.display ==
"none") {
document.getElementsByTagName(what).style.display = "";
alert('2getElement');
}
else { document.layers(what).style.display = ""; }
}
and if i swap getelem with d.all so getelem is first i says:

Error: document.getElementsByTagName(what).style has no
properties
The first part objs = function is obviously working as i know
all three browsers give alerts... so it just this bit above
that's messed up...

Thanks for your patience FredOZ, JS is not my natural
environment (ASP, SQL) and as much as i get it - i sometimes
dont! lol

----------------------------------------------
Posted with NewsLeecher v2.0 Beta 5
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------

Jul 23 '05 #6


InvisibleMan wrote:
Okay Netscape 7.2 is still jigged! - Javascript console says:

Error: document.all is not a function

if I make do:

if (document.all(what).style.display = "none") {
document.all(what).style.display = "";
alert('2all');
}
I told you need to check
if (document.all) {
// then use document.all here
}
otherwise you are asking for errors in browsers not supporting document.all.
And Fred is trying to tell you that you probably want comparison == and
not assigment = in the if condition.
else if (document.getElementsByTagName(what).style.display ==
"none") {
document.getElementsByTagName(what).style.display = "";
alert('2getElement');
}
else { document.layers(what).style.display = ""; }
}
and if i swap getelem with d.all so getelem is first i says:

Error: document.getElementsByTagName(what).style has no
properties


Well then that what argument is not a tag name of an element existing in
that document, the method
document.getElementsByTagName
takes a tag name like 'div' or 'span', if you are looking for an element
by its id e.g. if you have
<div id="divId">...</div>
then you need to do
var div;
if (document.getElementById) {
div = document.getElementById('divId');
}
else if (document.all) {
div = document.all['divId'];
}
if (div && div.style) {
if (div.style.display == 'none') {
div.style.display = '';
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #7
On Thu, 27 Jan 2005 14:24:05 GMT, InvisibleMan <in**********@nodomain.com>
wrote:

Continuing from Martin's comments...
function closeall()
{
var objs;

if (document.all)


In my opinion, you should test for standard properties or methods first.
The all collection is largely redundant now as the more recent IE versions
implement the DOM Core module (to some degree). It should be reserved for
IE4 and older browsers from other vendors that decided to follow Microsoft.

[snip]

An alternative approach is to make an attempt, such as yours, always
meaningful no matter what user agent encounters the document. Note that
"meaningful" is not the same as "successful". The most recent version of
my code (slightly altered) for this purpose is shown below. Note that this
is for show not use - a much more condensed version (at 431 bytes) is
included at the end of this post. Furthermore, the code is more about
speed (once the capabilities of the host have been tested) than size.

var getElementsByTagName = (function(g) {
/* Some basic type-testing functions */
function isFunction(o) {return 'function' == typeof o;}
function isGenericObject(o) {return isObject(o) || isFunction(o);}
function isNumber(o) {return 'number' == typeof o;}
function isObject(o) {return !!o && ('object' == typeof o);}
function isString(o) {return 'string' == typeof o;}

/* Attempts to determine if the object, o, is an element (and
* optionally if it is of type, t).
*/
function isElement(o, t) {
return isNode(o) && (1 == o.nodeType)
&& (!isString(t) || (t == o.nodeName));
}
function isNode(o) {
return isGenericObject(o) && isNumber(o.nodeType);
}

/* The function expression returned below tests the object passed
* as the first object.
*/
return function(o, tN) {
/* If the object supports the getElementsByTagName method, this
* will be used to obtain a return value.
*
* Note: IE evaluates the method as an object, hence the use of
* isGenericObject, rather than isFunction.
*/
var f = isGenericObject(o.getElementsByTagName)
? function(o, tN) {return o.getElementsByTagName(tN);}
/* If getElementsByTagName isn't supported, try using
* Microsoft's proprietary all collection.
*/
: (isObject(o.all) && isObject(o.all.tags))
? function(o, tN) {return ('*' == tN) ? o.all : o.all.tags(tN);}
/* If all of that fails, return an empty array. */
: function() {return [];};
/* If the object was an element or a reference to the document
* it can be assumed that the method determined by prior code
* can be used in future calls. This is achieved by replacing
* the testing code with the function created above.
*/
if(isElement(o) || (document == o)) {
g.getElementsByTagName = f;
}
return f(o, tN);
}
})(this);

It would be used thus:

/* Get all DIV elements in the document */
var elem = getElementsByTagName(document, 'div');

Although this sort of code might seem excessive, it balances out if you
use the function enough.

Mike
function isGenericObject(o){var t=typeof o;return('object'==t)||(
'function'==t);}function getElementsByTagName(o,tN){var f=
isGenericObject(o.getElementsByTagName)?function(o ,tN){
return o.getElementsByTagName(tN);}:(isGenericObject(o.al l)&&
isGenericObject(o.all.tags))?function(o,tN){return ('*'==tN)?o.all:
o.all.tags(tN);}:function(){return [];};if((o&&(1==o.nodeType))||(
document==o)){this.getElementsByTagName=f;}return f(o,tN);}

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #8
Is this not the comparison? which is what i thougt i'd put in my
last post - did see it did change it...

else if (document.getElementsByTagName(what).style.display ==
"none") {
document.getElementsByTagName(what).style.display = "";
alert('2getElement');
}

I'm not looking for the element, i'm looking for the tag...
because its the <div> i want to expand and close...

and is this not the check?:

if (document.all)
{ objs = document.all.tags("DIV");
}
else if (document.getElementsByTagName)
{ objs = document.getElementsByTagName("DIV");
}
else (document.layers)
{ objs = document.layers["DIV"];
}

Or am i just a plank! lol - sorry if tone seems a bit dispondant
but its really getting to me now... seems (probably is so
simple) and i'm kind pissed at myself - for, for all i can do
this is getting the better of me which i don't like!

Thanks again all

----------------------------------------------
Posted with NewsLeecher v2.0 Beta 5
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------

Jul 23 '05 #9
On Thu, 27 Jan 2005 17:23:05 GMT, InvisibleMan <in**********@nodomain.com>
wrote:
Is this not the comparison? which is what i thougt i'd put in my last
post - did see it did change it...

else if (document.getElementsByTagName(what).style.display ==
"none") {
It's *a* comparison, but it will produce a syntax error. The
getElementsByTagName method will return a collection, not an element
reference, so there will be no style object.
document.getElementsByTagName(what).style.display = "";
The same here.

It would seem you should be using the document.getElementById method.

if(document.getElementById) {
var e = document.getElementById(what), s;
if(e && (s = e.style) && ('none' == s.display)) {
s.display = '';
}
}

The gEBI method can also have the same treatment as getElementsByTagName
in my other post.

[snip]
is this not the check?: [...]


Yes, but the feature test only applies to that particular piece of code.
It doesn't automagically [sic] apply to all of the code in the document.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #10
On Fri, 28 Jan 2005 01:03:56 +1000, Fred Oz <oz****@iinet.net.auau> wrote:

[snip]
I'm not near IE right now, but I also think IE prefers square
brackets for document.all, not round:

{ objs = document.all.tags["DIV"];


Microsoft documentation actually uses parentheses rather than square
brackets, but IE accepts either. I refer the latter, personally.

[snip]
[Very unhelpful message]


This message is also backward - your 'Main Navigation system'
does not support the users browser. It would be helpful if you
told them which browsers you do support and offered links on
where to get them from.


It would be more helpful to make the navigation system usable without any
kind of scripting. Navigation is a basic feature within any web site and
it should never depend soley upon optional features, no matter how
"cross-browser" they are.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chris Leonard | last post: by
6 posts views Thread by David List | last post: by
12 posts views Thread by Kepler | last post: by
4 posts views Thread by ashkaan57 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.