Home arrow HTML arrow Page 2 - Preloading HTML Content with CSS
HTML

Preloading HTML Content with CSS


Website designers often use JavaScript to expose dynamic content to visitors. Sadly, updating JavaScript can be painful at best. CSS can help to overcome JavaScript's limitations -- and the two together open up some lovely possibilities. Alejandro Gervasio explains, with dynamic examples.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 37
January 12, 2005
TABLE OF CONTENTS:
  1. · Preloading HTML Content with CSS
  2. · Rotating HTML content with JavaScript
  3. · Hiding HTML content with CSS
  4. · Building a HTML content rotator with CSS
  5. · Building a simple drop-down menu with CSS
  6. · Code explanation to the rescue

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Preloading HTML Content with CSS - Rotating HTML content with JavaScript
(Page 2 of 6 )

Let’s say we have a set of messages for displaying in sequence, being commonly implemented as a JavaScript function, where each message is stored in an array structure and displayed one at a time. A typical solution might be coded in the following manner, first defining the JavaScript functions:

<script language="javascript">
// loads global functions
loadGlobalFunctions=function(){
 // rotates contents every 5 seconds
 rotateContent=function(){
  i++;
  if(i==message.length){i=0}
  container.innerHTML=message[i];
  setTimeout('rotateContent()', 5*1000);
 }
 // defines messages array
 var message=new Array();
 message[0]='<a href="http://www.google.com">Google takes you where you want!</a>';
 message[1]='<a href="http://www.devshed.com">Looking for high quality content? Visit Devshed.com now!</a>';
 message[2]='<a href="http://www.devarticles.com">Need for web development articles? Go Devarticles.com!</a>';
 var i=0;
 var container=document.getElementById('container');
 // execute rotateContent function
 rotateContent();
}
// execute code once page is loaded
window.onload=loadGlobalFunctions;
</script>

Nowt, we define our CSS declarations and HTML, respectively:

<style type="text/css">
.regular {
 font: bold 11px "Verdana", Arial, Helvetica, sans-serif;
 color: #000;
}
</style>

<p class="regular">Regular Content</p>
<br />
<div id="container" class="regular"></div>

Let’s break down the above script to understand how it works. In fact, it’s quite simple.

First, we defined the rotateContent() function, which takes care of transferring the content of each message, stored in the messages array, to a containing <div> element that will display them in turn.
We set a time interval of five seconds to show the content sequentially, only for example purposes. Then, we grasped the container <div> as an object and manipulated its content via the innerHTML property, which allows us to display each message alternatively.

Please note the line <div id="container" class="regular"></div>. It has no structural value, but refers to the containing <div> for displaying HTML content.

Finally, the script is executed once the page is loaded.

The working example can be viewed here: Example1

The complete code for this content rotator is as follows:

<html>
<head>
<title>CONTENT ROTATOR EXAMPLE</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script language="javascript">
// loads global functions
loadGlobalFunctions=function(){
 // rotate contents every 5 seconds
 rotateContent=function(){
  i++;
  if(i==message.length){i=0}
  container.innerHTML=message[i];
  setTimeout('rotateContent()', 5*1000);
 }
 // defines messages array
 var message=new Array();
 message[0]='<a href="http://www.google.com">Google takes you where you want!</a>';
 message[1]='<a href="http://www.devshed.com">Looking for high quality content? Visit Devshed.com now!</a>';
 message[2]='<a href="http://www.devarticles.com">Need for web development articles? Go Devarticles.com!</a>';
 var i=0;
 var container=document.getElementById('container');
 // execute rotateContent function
 rotateContent();
}
// execute code once page is loaded
window.onload=loadGlobalFunctions;
</script>
<style type="text/css">
.regular {
 font: bold 11px "Verdana", Arial, Helvetica, sans-serif;
 color: #000;
}
</style>
</head>
<body>
<p class="regular">Regular Content</p>
<br />
<div id="container" class="regular"></div>
</body>
</html>

As we can see with this approach, since all the messages are stored in variables, adding and updating content is an extremely cumbersome task. This old fashioned solution might be noticeably improved with the assistance of CSS for hiding regular HTML by using the capabilities of the CSS attribute display, as we’ll see in the next section.


blog comments powered by Disqus
HTML ARTICLES

- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5
- BBC Adopts HTML 5, Mozilla Addresses Issues
- Advanced Sticky Footers in HTML and CSS
- HTML and CSS Sticky Footers
- Strategy Analytics Predicts HTML5 Phones to ...
- HTML5 Guidelines for Web Developers
- Learning HTML5 Game Programming
- More Engaging CSS3 and HTML Background Effec...
- Engaging HTML and CSS3 Background Effects
- More Web Columns with CSS3 and HTML
- Columns with CSS3 and HTML
- Creating Inline-Block HTML Elements with CSS
- Drag and Drop in HTML5: Parsing Local Files

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials