0

What I want is to display one of, let's say, 10 different pieces of content (like an image or link or text or whatever) randomly when someone views the page.

I found this but it isn't working. I am on Wordpress and am using Code Insert Manager to use specific code on an individual page.

<SCRIPT LANGUAGE="JAVASCRIPT">


var r_text = new Array (); 
r_text[0] = "I was just thinking about you!"; 
r_text[1] = "You are a great example for others."; 
r_text[2] = "You have great ideas."; 
r_text[3] = "When I grow up I want to be you!"; 
r_text[4] = "I appreciate all of your opinions."; 

var i = Math.floor(r_text.length * Math.random()); 

document.write("<br /><br /><br /><br /><br /><br /><br /><center><FONT     SIZE=72><FONT COLOR='#FFFFFF'>" +     
r_text[i]  + "</FONT></center><br />"); 

var bgcolorlist=new Array("#228B22", "#FFD700", "#ADFF2F", "#FF69B4",     "#CD5C5C", "#4B0082", "#7CFC00", "#ADD8E6", "#E84643",     "#ED0A07", "#EA2907", "#E5294B", "#E00D26", "#FF3030", "#FC7500", "#F95700",     "#F43900", "#F95620") 

document.body.style.background=bgcolorlist[Math.floor(Math.random()*bgcolorli    st.length)]; 

</script>

But nothing is showing up on the page. I am completely unfamiliar with this kind of coding and could use some serious help modifying this so it works. I can fill in the content just fine, just need it to display first.

Lovely Books
  • 9
  • 1
  • 5

2 Answers2

0

You can try to start your script when DOM had loaded.

window.onload = function() {
  //you code here
};

If it will works for you, see here for more information window.onload vs document.onload

Community
  • 1
  • 1
user995045
  • 25
  • 3
0

It's working if you change 'bgcolorli st.length' to 'bgcolorlist.length'.

But.. Why do you want to do this using JavaScript? You can also do this with WordPress functions (or just plain php if you want) like get_posts(). It allows you to set a paramater called 'orderby' to 'rand' (which stands for random ;)

Sjors
  • 1,205
  • 1
  • 8
  • 24
  • Can you be more specfic? I just found the code on Google. Like I said, not very familiar with these kinds of coding. – Lovely Books Jan 19 '15 at 19:17
  • There are a few spaces between 'bgcolorli' and 'st.length'. If you remove them it works :) – Sjors Jan 19 '15 at 19:20
  • It isn't like that where I am using it. I am trying to set it up on a single page in wordpress using Code Insert Manager. Maybe I am implementing it wrong? Where would I place this code on a page, just in the HTML? – Lovely Books Jan 20 '15 at 03:45
  • Well, best is to use a code editor and just edit the theme files. Javascript goes just before the

    tag, other HTML can be anywhere in de body.

    – Sjors Jan 20 '15 at 05:27