Presenting information on the web can be a challenge, especially when you need to show more information than fits neatly on a given screen. This article presents a way to increase the density of information on the screen by rotating content within a fixed area dynamically. The popularity of rotating content areas indicates that users appreciate the value of having content on the page change dynamically. The trick is to change the content in such a way as not to distract the user. You can do that by limiting the area that changes, thus allowing users to view your content while simultaneously giving you a way to present more of the information that users crave.
Many Web sites possess an abundance of information?but screen real estate is at a premium. One method for presenting a lot of information in a small space is to create an area that rotates content within your page. So, the problem is, how do you create a rotating content area?
Write some simple object-oriented JavaScript, and a few style rules, combine them with a timer and some common-sense UI design guidelines, and you’re well on your way!
Create an HTML Page to Hold Rotating Content
To begin, you need an HTML page where you can experiment with rotating content. You can use the following simple page use the following code as a base HTML page for the sample project described in this solution. Open your favorite text editor and enter the following code.
News Rotator Today's News
The code used in this solution creates a news application, but the metaphor works equally well for any type of information your application requires. The only notable code in the HTML file is the
Create JavaScript News Items
Open a second file in your text editor to hold the JavaScript portion of the application. Following object-oriented principles, you’ll create a class that has properties appropriate for a news item. Add the following constructor to create a news item:
function makeNews(c,l,f,i){ this.copy = c; this.link = l; this.follow = f; this.img = i; this.write = writeNews; }
You call the makeNews method to create news objects. Each object takes four parameters: the copy (c), the link (l), the follow text (i.e. what to display as the link) (f), and an image (i). News objects also need a method to writing their content to the page. I’ve called that method write, and associated it with the writeNews function. Add the following writeNews function to your script:
function writeNews(){ var str = ''; str += ''; str += '
'; str += this.copy + '
'; str += '' + this.follow + ''; return str; }
As you can see, writeNews simply translates the properties of each news item into an HTML string. The string shown in the preceding code is arbitrary; you can modify the writeNews function to suit your needs as long as it writes well-formed HTML.
Because I wanted to be able to include an accompanying image in each news item’s display, I’ll provide image objects to force the browser to pre-load the images. It’s always a good idea to pre-load your images?especially if they aren’t visible when the page loads. Add the following code to pre-load the images:
var listImg = new Image(); listImg.src = 'watch.gif'; var treeImg = new Image(); treeImg.src = 'dhtml.gif'; var formImg = new Image(); formImg.src = 'form.gif'; var autoImg = new Image(); autoImg.src = 'web.gif';
Next, you need a collection of the news items to display on the page. I’ve used an array for the collection. Add the following to your script (each statement should be on a single line):
var newsArray = new Array(); newsArray[0] = new makeNews( "Move Items Between Lists With JavaScript", 'http://www.devx.com/GetHelpOn/10MinuteSolution/16372', 'Read More',listImg).write(); newsArray[1] = new makeNews( "Build an XML-Based Tree Control With JavaScript", 'http://www.devx.com/getHelpOn/Article/11874', 'More Info',treeImg).write(); newsArray[2] = new makeNews( "Automate Your Form Validation", 'http://www.devx.com/gethelpon/10MinuteSolution/16474', 'Full Story',autoImg).write(); newsArray[3] = new makeNews( "Create Fast, Smooth Multi-Page Forms With JavaScript", 'http://www.devx.com/webdev/Article/10483', 'More Info',formImg).write();
Note that you’re not really interested in storing the news objects themselves in the array; instead, you want the array to contain the results of the call to each news object’s write method. The preceding code stores the HTML string for each object in the array. That occurs because the new operator takes precedence over the call to the write method. In essence, the code first creates each news object, is created, and then calls its write method. The result is that the array stores the string returned from the call to each news object’s write method rather than the news objects themselves. Also note that I’ve passed in a reference to the pre-loaded image object for each item.
Write Code to Rotate News Items
It’s time to write the script that rotates the content. Remember, you’ve already set up the
var nIndex = 0; var timerID = null; function rotateNews(){ var len = newsArray.length; if(nIndex >= len) nIndex = 0; document.getElementById('stories').innerHTML = newsArray[nIndex]; nIndex++; timerID = setTimeout('rotateNews()',6000); }
As you can see, there isn’t anything really special going on here. The nIndex counter variable keeps track of which news item to display. When it gets to the end of the array, the code resets the counter to zero so that the process can start over?thus “rotating” the items in the array. The call to setTimeout keeps the function chugging along. Then, the code assigns the contents of one of the members of the array to the innerHTML property to the stories
function pauseNews() { if (timerID != null) { clearTimeout(timerID); timerID = null; } } function playNews() { if (timerID == null) { timerID = setTimeout('rotateNews()', 1000); } }
Save the file as rotate.js in the same folder as the HTML file.
Make It All Work
There are three tasks remaining. First, you need to include the script file you just created in the HTML file you created at the beginning of this solution. Add a reference to the script file in the section of the HTML document:
Next, you want to start the news items rotating when the page loads. To do that, attach a call to rotateNews to the onLoad event of the element:
Finally, you want to make the
Test your code by loading the HTML file into your browser. You’ll see the items rotate each time the timer fires. You can see that this approach lets you increase the amount of content available to your users and simultaneously adds visual appeal to the page. You can see a modified version of this script (I left out the images) at http://www.ncc.commnet.edu. That site needed a way to present new news items effectively and this script fit the bill nicely. This script has been tested in and works with IE6, Mozilla 1.4, and Opera 7.11.


What We Should Expect from Cell Phone Tech in the Near Future
The earliest cell phones included boxy designs full of buttons and antennas, and they only made calls. Needless to say, we’ve come a long way from those classic brick phones


The Best Mechanical Keyboards For Programmers: Where To Find Them
When it comes to programming, a good mechanical keyboard can make all the difference. Naturally, you would want one of the best mechanical keyboards for programmers. But with so many


The Digital Panopticon: Is Big Brother Always Watching Us Online?
In the age of digital transformation, the internet has become a ubiquitous part of our lives. From socializing, shopping, and learning to more sensitive activities such as banking and healthcare,


Embracing Change: How AI Is Revolutionizing the Developer’s Role
The world of software development is changing drastically with the introduction of Artificial Intelligence and Machine Learning technologies. In the past, software developers were in charge of the entire development


The Benefits of Using XDR Solutions
Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved


How AI is Revolutionizing Fraud Detection
Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across


Companies Leading AI Innovation in 2023
Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several


Step-by-Step Guide to Properly Copyright Your Website
Creating a website is not easy, but protecting your website is equally important. Implementing copyright laws ensures that the substance of your website remains secure and sheltered. Copyrighting your website


Fivetran Pricing Explained
One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of


Kubernetes Logging: What You Need to Know
Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes


Why Is Ransomware Such a Major Threat?
One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing


Tools You Need to Make a Data Dictionary
Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as