Networking, Programming and Graphics - Tutorials
ONLINEHOWTO.net Tutorials Category

Create JavaScript Testimonials

Type: Code Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Level: Beginner Networking, Programming and Graphics - Tutorials 
Networking, Programming and Graphics - Tutorials
Date: 2009-Mar-04
Networking, Programming and Graphics - Tutorials
Visited: 2909 times
Networking, Programming and Graphics - Tutorials
Rating: Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Published: Ivory Morhuld

Many websites have special section for displaying Testimonials and other greetings from visitors, users, clients, partners, etc . This will show on new visitors how useful an service or product and what benefit visitors can get when using it.

With this tutorial I will show you how to create simple Java Script Testimonials for your website.
Main concept for displaying testimonials is to place all testimonial items in some database, array or file. Next with some random function to get one item and to show it in website.

First we have to prepare all testimonials to be well organized for fast and easy extraction. For this case we will use simple Java Script Array
var testimonials=['Thanks. Have a Nice Day!', 'Very useful Website', 'Greetings from NY', 'Go Yonkees! Go'];
Now we have to prepare function for getting random testimonial / recommendation from the array. This function will use our already prepared array with some testimonials and using Math.random function will select one testimonial and return it for later displaying. This function have to be positioned between <script type="text/javascript"> and </script> tags.
function GetRandomTestimonials() {
    TestimonialsQty = testimonials.length;
    randomPos = Math.floor(Math.random()*TestimonialsQty);
    return testimonials[randomPos];
}
Displaying the result is quite easy with document.write(); function. This have to be positioned between <script type="text/javascript"> and </script> tags and on suitable place on the website.
document.write(GetRandomTestimonials());
Now we will place array with testimonials, function for getting random testimonial and exact displaying of testimonials in separate <script type="text/javascript"> ... </script> section and all of this in one simple html file to test the entire functionality. Your html file have to look like this:
<html>
<head>
<title>Displaying Java Script Testimonials</title>
<script type="text/javascript">
var testimonials=['Thanks. Have a Nice Day!', 'Very useful Website', 'Greetings from NY', 'Go Yonkees! Go'];

function GetRandomTestimonials() { 
    TestimonialsQty = testimonials.length; 
    randomPos = Math.floor(Math.random()*TestimonialsQty); 
    return testimonials[randomPos]; 
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(GetRandomTestimonials());
</script>
</body>
</html>
Now lets check how all of this will work. Click on [Refresh] button or press F5 (Refresh) to get random testimonial / recommendation.
Displaying Java Script Testimonials

Rate this tutorial:                    
Post Comment

    • ( jack . otieno [ at ] ymail . com ),

      posted on 2009-Sep-23 | 07:07:07 AM
      This code is nice. Goog enough for my project
    • ( sri [ at ] yaho . com ),

      posted on 2010-Feb-19 | 02:57:23 AM
      Thank you
    • ( rafael [ at ] gmail . com ),

      posted on 2010-Apr-26 | 03:06:31 PM
      Hola Saludos
    • ( toko_vcc [ at ] yahoo . com ),

      posted on 2010-Aug-22 | 11:16:01 PM
      how to write post comment table java script codes? please email me..thanks
Need a specific tutorial? Do not hesitate and submit a request!
Your e-mail: