★ wanayoo — archive 1999 http://javascript.about.com/compute/javascript/library/weekly/aa071199.htmNouvelle recherche | Portail wanayoo
   You are here:    About.com > Computing/Technology > Focus on JavaScript > Articles
spacer
shadow
shadow
spacer Home
Thu, Mar  2,  2000
spacer
graphic
Join Free
Your Guide Nate Kassebaum - your About.com Guide to:
Focus on JavaScript

Content: Welcome | Netlinks | Articles | Guide Bio | Search | Related
TalkAbout: Forums | Chat | Events | Newsletter | Share This Site | Join
Shopping: ShopNow | Books | Videos | Jobs | ShoppingAbout

Cross-Browser Layers

Dateline: 07/11/99

The trouble with making high-quality cross-browser Dynamic HTML is that Netscape and Microsoft have very different implementations of layers.

According to the W3C standards recommendation, the correct way to implement layers is by using <DIV> and <SPAN> tags (do not use the Navigator <LAYER> tag).  This is all right, because Internet Explorer 4.0+ and Navigator 4.06+ support all the <DIV> and <SPAN> functionality we need.  The only trouble is that they do it so differently.

Internet Explorer allows you to access any layer (really, any object that as an ID tag) using document.all.  Take the following example code:

<html>
<head><title>Hello</title></head>
<body>

<div id="Layer1">
This is in Layer 1

<div id="Layer2">
This is in Layer 2
</div>

</div>

</body>
</html>

To access the properties of Layer1 in Internet Explorer, you would use:

document.all.Layer1.property

To access the properties of Layer2, you would use:

document.all.Layer2.property

Pretty straightforward, and it makes plenty of sense to do it this way.  However, if you want to access these layers in Netscape Navigator, you'll have to use statements like this:

document.layers.Layer1.property

or

document.layers.Layer1.layers.Layer2.property 

This is also pretty straightforward, and makes just as much sense as the Internet Explorer way of doing things.  However, the two ways of accessing layers are fundamentally different.  In Internet Explorer, all layers in the document are accessible under document.all.  In Navigator, the layers form a hierarchy.  This means that Layer1 is accessible under document.layers, but Layer2 is not.  Layer2 is only accessible under document.layers.Layer1.layers.  What I mean is that

document.layers.Layer2

is not defined, because it is a child of document.layers.Layer1 in Navigator.

Writing code that works under either one of these models is a cinch, but writing code that works under both of these models simultaneously can quickly become a huge undertaking.

A pretty good solution to this problem is to find the layers on-the-fly using a custom function (which I'll show you in a minute).  For example, you could say:

theLayer = findLayer("Layer2");
theLayer.property = property;

Where the findLayer function would find the layer called "Layer2" using document.all if we're in Internet Explorer, and recursively if we're in Navigator.

Now, here is the code for the findLayer function:

function findLayer(layerName, currLayer)
{
 if (document.all && document.all.layerName) 
  return document.all.layerName;
 var layerFound = "";
 if (currLayer[layerName] != null) 
  return currLayer[layerName];
 else
 {
  if (currLayer.layers && currLayer.layers.length > 0)
  {
   for (i = 0; i<currLayer.layers.length; i++)
   {
    layerFound = findLayer(layerName, currLayer.layers[i]);
    if (layerFound) return layerFound;
   }
  }
 }
 return layerFound;
}

More information:
SiteExperts.com: Cross-Browser DHTML

Visit the message boards!

Previous Features

About.com Related About.com GuideSites
  • Focus on Java
  • Focus on Web3D
  • HTML
  • Intranets
  •  
  • Perl
  • Personal Web Pages
  • Web Design
  • XML
  •  
    About.com Elsewhere on About.com
    What's New, What's Hot
  • Breaking News
  • Decorate Your Dorm Room
  • Conquer Jet Lag
  •   JustAbout
  • Quit Smoking
  • Healthy Living
  • Twisted Travel

  • search Search
      
    This Site About.com
    Share This Site With a Friend (enter their email address below):

       

    Top of Page

    spacer
    In Partnership With TechWeb
    TechWeb News
     TechWeb News
    Kinko's Forms Web Venture

    Cisco Snaps Up Network-Management Vendor

    Ariba CEO: People Are Key To B2B
    Top Downloads
     Free Downloads
     


    Marketplace

    ShoppingAbout

    Find a Friend BookStore Build a Web Page Finance Channel Send a card! Sweepstakes
    shadow
    shadow

    shadow
    shadow

    © 2000 About.com, Inc. All rights reserved.
    ©   2000 About.com, Inc. "MiningCo.com" and "About.com" are trademarks of About.com, Inc.,
    a publicly traded company listed on NASDAQ under the symbol "BOUT."
    For more information on About.com, visit Our Story, Be a Guide, Be an Advertiser, or Investor Relations.
    For rules of use, read our User Agreement; for privacy concerns, read our Privacy Policy.
    Having a Problem? Report it here.