var favorites = ["http://google.com", "http://yahoo.com", "http://msn.com", "http://apple.com"]; var favorite = favorites[Math.floor(Math.random() * favorites.length)]; var postmessage = "hi my favorite site is " + favorite;
Create an array of your sites, then select one element from the array. You do this by choosing a random number using Math.random() , which gives a result greater than or equal to 0 and less than 1. Multiply by the length of your array and take floor (i.e. take only the integer part, omitting any decimal points) so that you will have a number from 0 to one less than the length of your array (which will thus be a valid index in your array). Use this result to select an element from your array.
source share