473,573 Members | 4,155 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

document.layers error in IE

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 {
display: none;
}
..menu {
display: block;
}
</style>

<script language="javas cript>
if (document.getEl ementById)
{
function getElemById(id)
{
return document.getEle mentById(id);
}
}
else if (document.all)
{
function getElemById(id)
{
return document.all(id );
}
}
else if (document.layer s)
{
function getElemById(id)
{
return document.layers[id];
}
}

function hide(ele)
{
getElemById(ele ).className = "noshow";
}

function show(ele)
{
getElemById(ele ).className = "menu";
}
</script>

Then in the html part:

<a href="javascrip t:show('categor y');">Show Categories</a>

<div id="category" class="noshow">
....
....
<a href="javascrip t:hide('categor y');">Hide Categories</a>
</div>

TIA.

Jul 23 '05 #1
4 3787
OK, I am a bit confused!! After posting this I checked and it seems
that document.layers is part of netscape 4 and document.all is for IE
4/5. I tested this using IE6, which should work with
document.getEle mentById. So, why is IE6 using the function related to
if (document.layer s)???!!

as*******@hotma il.com wrote:
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 {
display: none;
}
.menu {
display: block;
}
</style>

<script language="javas cript>
if (document.getEl ementById)
{
function getElemById(id)
{
return document.getEle mentById(id);
}
}
else if (document.all)
{
function getElemById(id)
{
return document.all(id );
}
}
else if (document.layer s)
{
function getElemById(id)
{
return document.layers[id];
}
}

function hide(ele)
{
getElemById(ele ).className = "noshow";
}

function show(ele)
{
getElemById(ele ).className = "menu";
}
</script>

Then in the html part:

<a href="javascrip t:show('categor y');">Show Categories</a>

<div id="category" class="noshow">
...
...
<a href="javascrip t:hide('categor y');">Hide Categories</a>
</div>

TIA.


Jul 23 '05 #2


as*******@hotma il.com wrote:
OK, I am a bit confused!! After posting this I checked and it seems
that document.layers is part of netscape 4 and document.all is for IE
4/5. I tested this using IE6, which should work with
document.getEle mentById. So, why is IE6 using the function related to
if (document.layer s)???!!

as*******@hotma il.com wrote:
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 {
display: none;
}
.menu {
display: block;
}
</style>

<script language="javas cript>
if (document.getEl ementById)
{
function getElemById(id)
{
return document.getEle mentById(id);
}
}
else if (document.all)
{
function getElemById(id)
{
return document.all(id );
}
}
else if (document.layer s)
{
function getElemById(id)
{
return document.layers[id];
}
}


....
Take a look at this:

if( true ) {
function myFunc( ) {
thing1();
}
} else if( false ) {
function myFunc( ) {
thing2();
}
}

It won't do what you expect, because function declarations in the form
of:

function identifier( ) { statements }

are parsed out before any logic is performed.

It's the same as saying:
function myFunc( ) {
thing1();
}

function myFunc( ) {
thing2();
}

And because the second declaration overwrites the first, you might as
well just say:
function myFunc( ) {
thing2();
}
Instead, consider declaring getElemById() this way:
getElemById = function( id ) {
// statements
}

---------------------------
If you rewrite it this way, it should work as you expect:

if( document.getEle mentById )
getElemById = function( id ) {
return document.getEle mentById( id );
}

else if( document.all )
getElemById = function( id ) {
return document.all[ id ];
// note that we use square brackets here
}

else if( document.layers )
getElemById = function( id ) {
return document.layers[ id ];
}

Jul 23 '05 #3
On 29/05/2005 10:51, Random wrote:
as*******@hotma il.com wrote:
it seems that document.layers is part of netscape 4
Amongst some others, but generally yes, and probably not worth worrying
about.
and document.all is for IE 4/5.

The all collection is supported by all IE versions after 4.0, as well as
other user agents. However, for the purposes of obtaining a single
element reference, its use should be restricted to IE 4 only. The
document.getEle mentById method is preferable.

[ashkann:]
<style>
[snip]
<script language="javas cript>


The type attribute is required by both elements:

<style type="text/css">

<script type="text/javascript">

[snip]
if( true ) {
function myFunc( ) {
thing1();
}
} else if( false ) {
function myFunc( ) {
thing2();
}
}
[snip]
It's the same as saying:
function myFunc( ) {
thing1();
}

function myFunc( ) {
thing2();
}


No, it's not. Depending on your point of view, at best the former should
throw a syntax error, and at worst it will act like the latter.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
<as*******@hotm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
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 {
display: none;
}
.menu {
display: block;
}
</style>

<script language="javas cript>
if (document.getEl ementById)
{
function getElemById(id)
{
return document.getEle mentById(id);
}
}
else if (document.all)
{
function getElemById(id)
{
return document.all(id );
}
}
else if (document.layer s)
{
function getElemById(id)
{
return document.layers[id];
}
}

function hide(ele)
{
getElemById(ele ).className = "noshow";
}

function show(ele)
{
getElemById(ele ).className = "menu";
}
</script>

Then in the html part:

<a href="javascrip t:show('categor y');">Show Categories</a>

<div id="category" class="noshow">
...
...
<a href="javascrip t:hide('categor y');">Hide Categories</a>
</div>


Lots of people have explained why you are seeing what you are seeing,
but I don't think anyone has given you a clear way to resolve it:

<script type="text/javascript">
var d = document;
var gEBI;
if (d.getElementBy Id) {
gEBI = function(id) { return d.getElementByI d(id); }
} else if (d.all) {
gEBI = function(id) { return d.all(id); }
} else {
gEBI = function() { return { style:{} }; }
}
function hide(id) { gEBI(id).classN ame = "noshow"; }
function show(id) { gEBI(id).classN ame = "menu"; }
}

Because you use -gEDI- without testing what it returns a generic version
of -gEBI- that returns a new Object is required so that you can assign
a -className- property to it without causing an error.

Since assigning -className- to the object returned by
document.layers[id] doesn't do anything either, I removed that test and
have only two tests for document.getEle mentById and document.all.
Netscape 4 will use the generic function that returns a "fake" node.

The reason I have the generic version of the function return {
style:{} } is so you can do things like:

gEBI(id).style. display = 'none';

and it won't cause an error in Netscape 4 (it won't do anything either,
but presumably you have graceful degradation for Netscape 4 anyway).

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1768
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
6829
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 these problems is using document.layers. I have Google'd for examples of how to use the document object specifically with Mozilla, but I cannot find...
3
3319
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 just the entire 4.X series. Script works fine under IE. I've tried a few things to get it working under Netscape, such as defining a style for the...
1
9530
by: Leila | last post by:
Folks I have an html file which looks like this: .. .. .. <body onLoad="WindowOnLoad();"> .. ..
12
10142
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 scrollbar, you get the height of the entire document, screwing up any chance of centering a window in the browser using these values. Is there a...
3
2433
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: <div class="small" style="display: none" id="menu1_menu"> when the heading is clicked: <a href=.. onClick="expandDiv('menu1')">
10
54858
by: InvisibleMan | last post by:
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;
6
1812
by: Aaron Gray | last post by:
Hi, I know the 'document.getElementById()' issue is really over since every browser since 1998 supports the W3C DOM standard, but I could not resist this offering :- if (!document.getElementById) document.getElementById = function(id) { return document.all; } This should then provide a getElementById function if one does not already
7
8089
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 function toggleDiv(id,flagit) {
0
7789
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8037
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7800
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5296
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3737
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3743
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2227
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1048
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.