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.
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:
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 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.