Website Design Edmonton       Website Design Feedback       Website Design Sitemap     Contact Website Design Company
Website Designers R Us - Home
Rotating HTML content with JavaScript

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.

 

Return to Listing

Website Designers R Us is a professional Internet strategy and Web design company based in Toronto, Canada. We specialize in best-in-class website design, online marketing, Flash multimedia, corporate identity and print graphics. Website Designers R Us features an integrated team of web consultants, creative designers, writers, programmers and marketing professionals that know how to get online results
 Home          ::             About Us          ::             Support            ::             Services            ::             Link Partners          ::             Contact

Copyright © 2006-2011 Website Designers R Us, a DOT Specialist Company. All rights reserved.