Economica
09-30-2012, 12:45 AM
Hi, I'm new to the forum! :) Don't know why I haven't discovered this place before. I have amateurish HTML/CSS skills (showcased at my site (http://www.celebritytypes.com/philosophers/index.php)); as far as JavaScript goes I'm at the stage where all I do is follow instructions at e.g. JavaScriptKit.com without actually understanding the script. :o Okay, enough throat-clearing...
My question concerns randomized content. I have ten quotes in total. I want to show five quotes at a time and I have been able to randomize two blocks of five quotes using this script (http://www.javascriptkit.com/script/script2/imageorder.shtml). So far so good. However, I'd also like to randomize the order of those five quotes within each block, and I can't figure out how to modify the script or combine it with another one in order to do that. Please help? :confused:
Here is the meat of the script (please bear with me if I'm shortening it too much or too little):
var randomordercontentdisplay={
divholders:new Object(),
masterclass: "randomordercontent",
init:function(){
if (!document.getElementById)
return
var alldivs=document.getElementsByTagName("div")
var randomcontentsearch=new RegExp(this.masterclass+"\\s+(group\\d+)", "i") //check for CSS class="randomcontent groupX" (x=integer)
for (var i=0; i<alldivs.length; i++){
if (randomcontentsearch.test(alldivs[i].className)){
if (typeof this.divholders[RegExp.$1]=="undefined"){ //if object to hold this group of divs doesn't exist yet
this.divholders[RegExp.$1]=new Object() //create object
this.divholders[RegExp.$1].ref=[] //create array to hold each div within group
this.divholders[RegExp.$1].contents=[] //create array to hold each div's content within group
}
this.divholders[RegExp.$1].ref.push(alldivs[i]) //add this div to the array
this.divholders[RegExp.$1].contents.push(alldivs[i].innerHTML) //add this div's content to the array
}
}
this.scrambleorder()
},
scrambleorder:function(){
for (group in this.divholders){ //loop thru each array within object
this.divholders[group].contents.sort(function() {return 0.5 - Math.random()}) //scramble contents array
for (var i=0; i<this.divholders[group].ref.length; i++){
this.divholders[group].ref[i].innerHTML=this.divholders[group].contents[i]
this.divholders[group].ref[i].style.display="block"
}
}
}
}
My question concerns randomized content. I have ten quotes in total. I want to show five quotes at a time and I have been able to randomize two blocks of five quotes using this script (http://www.javascriptkit.com/script/script2/imageorder.shtml). So far so good. However, I'd also like to randomize the order of those five quotes within each block, and I can't figure out how to modify the script or combine it with another one in order to do that. Please help? :confused:
Here is the meat of the script (please bear with me if I'm shortening it too much or too little):
var randomordercontentdisplay={
divholders:new Object(),
masterclass: "randomordercontent",
init:function(){
if (!document.getElementById)
return
var alldivs=document.getElementsByTagName("div")
var randomcontentsearch=new RegExp(this.masterclass+"\\s+(group\\d+)", "i") //check for CSS class="randomcontent groupX" (x=integer)
for (var i=0; i<alldivs.length; i++){
if (randomcontentsearch.test(alldivs[i].className)){
if (typeof this.divholders[RegExp.$1]=="undefined"){ //if object to hold this group of divs doesn't exist yet
this.divholders[RegExp.$1]=new Object() //create object
this.divholders[RegExp.$1].ref=[] //create array to hold each div within group
this.divholders[RegExp.$1].contents=[] //create array to hold each div's content within group
}
this.divholders[RegExp.$1].ref.push(alldivs[i]) //add this div to the array
this.divholders[RegExp.$1].contents.push(alldivs[i].innerHTML) //add this div's content to the array
}
}
this.scrambleorder()
},
scrambleorder:function(){
for (group in this.divholders){ //loop thru each array within object
this.divholders[group].contents.sort(function() {return 0.5 - Math.random()}) //scramble contents array
for (var i=0; i<this.divholders[group].ref.length; i++){
this.divholders[group].ref[i].innerHTML=this.divholders[group].contents[i]
this.divholders[group].ref[i].style.display="block"
}
}
}
}