Tuesday, April 9, 2013

How the Quote Randomization works

The text randomization in the header of my page is a simple piece of javascript.

<script type="text/javascript">
var textarray = [
"Jazz is known all over the world as an American musical art form and that's it. No America, no jazz. I've seen people try to connect it to other countries, for instance to Africa, but it doesn't have a damn thing to do with Africa. - Art Blakey",
"I'm always thinking about creating. My future starts when I wake up every morning... Every day I find something creative to do with my life. - Miles Davis",
"I believed in studying just because I knew education was a privilege. It was the discipline of study, to get into the habit of doing something that you don't want to do. - Wynton Marsalis",
"To be a true artist you have to play the way you feel - not the way others think you should feel. - Don Ellis",
"I say, play your own way. Don’t play what the public wants. You play what you want and let the public pick up on what you’re doing? even if it does take them fifteen, twenty years. - Thelonius Monk",
"Let my children have music! Let them hear live music. Not noise. My children! You do what you want with your own! - Charles Mingus",
"But, I don't think any arranger should ever write a drum part for a drummer because if a drummer can't create his own Interpretation of the chart and he plays everything that's written, he becomes mechanical; he has no freedom. - Buddy Rich" // No comma after last entry
];

function RndText() {
var rannum= Math.floor(Math.random()*textarray.length);
document.getElementById('ShowText').innerHTML=textarray[rannum];
}
onload = function() { RndText(); }

</script>



To make it work, you put each possible string in quotes and separate them by commas. To determine the ID you need to get the stuff to display, you fill in the variable in document.getElementID('ShowText'). I just left it with ShowText, but you could change it if you want to. Stick all that in the head of your code.

To make it display, you enter <div id="showtext"></div> somewhere in the body of your code. In my page, it's inside another div used to contain it.

The code itself generates a random number, multiplies it by the number of elements in the array, and then uses that result to pick a number out of the array.

You should be able to just copy/pasta the code above and fill in your own text.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.