Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

load random divs

Status
Not open for further replies.

Diggum1

Programmer
Oct 14, 2004
67
US
Anyone know how to achieve the following:

I have several testimonial divs, each with different content, like this:

<div ID="alice">Alice's testimonial.
Lots of other HTML</div>

<div ID="bill">Bill's testimonial.
Lots of other HTML</div>

<div ID="sandy">Sandy's testimonial.
Lots of other HTML</div>

I'd like to randomly load (on every page refresh)those divs into a <td> on my homepage, but I'm not sure how to do this.

Better yet, I'd like to place each of these divs into their own .aspx page, and randomly <include> them into that same <td> on my homepage

Any ideas? Any javascript (or .net) solutions welcome!

Thanks
Rick

 
Rather than load all the testimonials onto the client's page, I'd number the testimonials and put them into one separate aspx page - serving the appropriate testimonial based on the specified id. So a request to testimonial.aspx?id=3 will serve up only Testimonial #3. Then on the page that is to display them, I would use an AJAX call to fill the div where the testimonial will go. Such as the following...(warning! I haven't tested this!)
Code:
window.onload = requestTestimonial;

var req = false;
function requestTestimonial(){
  var num_testimonials = [b]20[/b];
  var random_num = Math.round((Math.random()*num_testimonials)+1);

  if(window.XMLHttpRequest){
    req = new XMLHttpRequest();
  } else if(window.ActiveXObject) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if(!req)return;

  req.onreadystatechange = testimonalCallback;
  req.open('GET','[b][URL unfurl="true"]http://yourdomain.com/testimonial.aspx?id=[/URL][/b]'+ random_num, true);
  req.send();
}

function testimonialCallback(){
  if(req.readyState==4){
    if(req.status==200){
      document.getElementById("[b]TestimonalDiv[/b]").innerHTML = req.response.Text;
      [green]// setTimeout('requestTestimonial()',10000);[/green]
    }
  }
}
You can uncomment the last line to make it loop every 10 seconds.

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top