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: 5 stars5 stars5 stars5 stars5 stars / 48
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

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

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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