473,396 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 54825


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rosinger | last post by:
Hi all, What is the preferred in term of browser copatability to get a reference to an element in the document in JavaScript? I am not sure about when to use document.all or document.layers or...
1
by: Chris Leonard | last post by:
Hi. Can anyone help me please. If the syntax for IE is: document.getElementById(layerID) What if I want to use layers ? document.layers ?????????
6
by: David List | last post by:
I'm having a problem using different properties of the document object in the example javascripts in my textbook with browsers that identify themselves as using the Mozilla engine. One example of...
3
by: Crimefighter | last post by:
I'm not a javascript guy, but the use of this banner rotator script has given me fits. I know a few causes of the problem thus far, one being the layer tags aren't supported under Netscape 7.1...
12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
3
by: InvisibleMan | last post by:
Thanks in Advance for any help on this - its truely sending my head in loops... so I appreciate your efforts! okay, I have a javascript listed below that drops down submenus contained within:...
4
by: ashkaan57 | last post by:
Hi, I am using the following code to show/hide part of an html page. It works in Netscape and Firefox but dies in IE: "Error: document.layers is null or not an object" <style> ..noshow {...
5
by: marcus6 | last post by:
Howto access xmlHttp.responseText as document.getElementByTagName ? If I access a .html page through the xmlHttp, how can I use the functions .getElementsByTagName and .getElementById on the data...
7
by: sunny1009 | last post by:
Hi Guys, Can you please check this code and see whats wrong with it?? I am getting document.all.style is null or not an object error. Any help will be really appreciated. Thanks Code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.