Re-Write a Layer’s Content with Javascript

Share this article

One of the most common tasks Web developers face every day is to change the content of a Web page, without additional requests to the Web server. The easiest way to accomplish this assignment is through the use of layers.

Here, I’ll show you how to re-write a layer’s content with a simple function that can be re-used over and over in your Javascript code. This function works in both major browsers – Netscape 4.*/6/7 and IE 4/5/6. Consider this code fragment:

<DIV ID="MyLayer" style="position:absolute;top:10px; 
left:10px;">Initial layer text</DIV>

<script language="Javascript">
function WriteLayer(ID,parentID,sText) {
 if (document.layers) {
   var oLayer;
     oLayer = eval('document.' + parentID + '.document.' + ID + '.document');
     oLayer = document.layers[ID].document;
 else if (parseInt(navigator.appVersion)>=5&&navigator.
appName=="Netscape") {
   document.getElementById(ID).innerHTML = sText;
 else if (document.all) document.all[ID].innerHTML = sText

<input type="button" value="Display Time" onclick="WriteLayer

Let’s take a closer look at the WriteLayer() function first. The function takes 3 parameters: ID, parentID and sText. ID is the ID of our div tag — in our case, “MyLayer“. The second parameter, parentID, is needed because of the way Netscape 4.* DOM works with nested layers. These are simply “layers within layers”. An example of nested layers is:

"ParentLayer"><div ID= "ChildLayer"></DIV></DIV>

You don’t have direct access to a nested layer in Netscape 4.*. The only way to access it is through its parent layer:

var oChildLayer = document.ParentLayer.document.ChildLayer.document;

When we call WriteLayer() function, the parentID parameter should be null if the layer that we want to re-write is not nested in the other layer. If the layer is nested, the parentID should be the same as the parent layer ID.

The third parameter, sText, is simply the new layer content.

Our WriteLayer() function uses browser-specific code to handle the layer re-writing. If the browser is Netscape 4.* then the following code is executed:

var oLayer; 
 oLayer = eval('document.' + parentID + '.document.' + ID + '.document');
 oLayer = document.layers[ID].document;

In the first step we declare the oLayer variable that will contain a reference to our layer. In the next step we check if the value of the parentID is null. If it isn’t, the following line is executed:

oLayer = eval('document.' + parentID + '.document.' + ID + '.document');

For those of you who aren’t familiar with the JavaScript eval() function, I’ll explain this line in a little more detail.

The argument of the eval() function must be a string that can be executed as a valid JavaScript statement. If the string represents an expression, eval() evaluates the expression. If the argument represents one or more JavaScript statements, eval() performs the statements. If we call our WriteText() function with the following parameters:

WriteLayer("ChildLayer","ParentLayer","Some text...")

then the expression will be evaluated to:

oLayer = document.ParentLayer.document.ChildLayer.document;

If the parentID parameter is null then the layer can be accessed directly:

oLayer = document.layers[ID].document;

After we’ve gained a reference to our layer, we simply open the layer’s document object, write the sText value in it, and close the document.; 



Re-writing a layer content in Netscape 6/7 is much easier. All we need is to assign the sText value to the innerHTML property of our layer, which is accessed with getElementById method:

document.getElementById(ID).innerHTML = sText;

Re-writing a layer content in IE is easy too, because the only thing that we need to do is assign the sText value to the innerHTML property of our layer, which is accessed through the all collection:

document.all[ID].innerHTML = sText;

You can use WriteLayer() function to write any valid HTML code into the layer.

To see our WriteLayer() function in action click here.

Frequently Asked Questions about JavaScript Content Layers

What is the concept of content layers in JavaScript?

Content layers in JavaScript refer to the different levels of content organization within a web page. These layers include the HTML layer, CSS layer, and JavaScript layer. The HTML layer is responsible for the structure and content of the web page, the CSS layer handles the presentation and layout, while the JavaScript layer adds interactivity and dynamic elements to the web page. Understanding these layers is crucial for effective web development as it allows for better organization, easier debugging, and more efficient coding.

How does JavaScript interact with HTML and CSS layers?

JavaScript interacts with HTML and CSS layers through the Document Object Model (DOM). The DOM is a programming interface that allows JavaScript to manipulate the content, structure, and style of a web page. JavaScript can create, delete, and change HTML elements, set CSS styles, and react to existing webpage events, making the web page more interactive and dynamic.

What are the benefits of understanding content layers in JavaScript?

Understanding content layers in JavaScript provides several benefits. It allows developers to write cleaner, more organized code, making it easier to debug and maintain. It also enables developers to create more interactive and dynamic web pages, enhancing the user experience. Additionally, understanding content layers can lead to better performance as developers can optimize each layer for its specific purpose.

Can I use JavaScript without understanding content layers?

While it’s possible to use JavaScript without a deep understanding of content layers, it’s not recommended. Understanding content layers allows you to write more efficient, organized, and maintainable code. It also helps you to better understand how JavaScript interacts with HTML and CSS, which is crucial for creating dynamic and interactive web pages.

How can I start learning about content layers in JavaScript?

There are many resources available to learn about content layers in JavaScript. Online tutorials, coding bootcamps, and textbooks are all good places to start. Practice is also crucial. Try building simple web pages and gradually add more complex features as you become more comfortable with the concept of content layers.

What are some common mistakes when working with content layers in JavaScript?

Some common mistakes when working with content layers in JavaScript include not properly separating concerns between the layers, over-reliance on JavaScript for tasks that could be handled by HTML or CSS, and not fully understanding how the DOM works. These mistakes can lead to messy, inefficient code and poor performance.

How does understanding content layers contribute to better web development practices?

Understanding content layers contributes to better web development practices by promoting clean, organized code. It encourages the separation of concerns, where each layer is responsible for a specific aspect of the web page. This makes the code easier to maintain and debug. It also leads to better performance as each layer can be optimized for its specific task.

Can I use JavaScript libraries and frameworks without understanding content layers?

While JavaScript libraries and frameworks can simplify the development process, a solid understanding of content layers is still important. These tools often abstract away some of the complexities of working with content layers, but understanding the underlying principles can help you use these tools more effectively and troubleshoot any issues that may arise.

How does JavaScript handle the interaction between different content layers?

JavaScript handles the interaction between different content layers through the DOM. It can access and manipulate HTML elements, change CSS styles, and respond to user events. This allows JavaScript to add interactivity and dynamic content to a web page, enhancing the user experience.

What are some best practices when working with content layers in JavaScript?

Some best practices when working with content layers in JavaScript include keeping a clear separation of concerns between the layers, understanding how the DOM works, and using JavaScript to enhance the user experience rather than relying on it for basic functionality. It’s also important to keep your code organized and maintainable, and to optimize each layer for its specific task.

Peter TodorovPeter Todorov
View Author

Peter runs a Windows hosting company, which offers affordable ASP and ASP.NET hosting. He started recently featuring ASP and ASP.NET articles and tutorials.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week