1 2 3 4 5 6 7 8 9 10 11 12
<script type="text/javascript"> var feature_stuff = new Array( '<li><a href="#">URL 1</a></li>', '<li><a href="#">URL 2</a></li>', '<li><a href="#">URL 3</a></li>', '<li><a href="#">URL 4</a></li>', '<li><a href="#">URL 5</a></li>' ); var rand = Math.floor(Math.random()*feature_stuff.length); feature_stuff = feature_stuff[rand]; document.write(feature_stuff); </script>
Refactorings
No refactoring yet !
Jon
October 29, 2007, October 29, 2007 18:25, permalink
1 2 3 4 5 6 7 8 9 10 11
<script type="text/javascript"> var feature_stuff = new Array( '<li><a href="#">URL 1</a></li>', '<li><a href="#">URL 2</a></li>', '<li><a href="#">URL 3</a></li>', '<li><a href="#">URL 4</a></li>', '<li><a href="#">URL 5</a></li>' ); var rand = Math.floor(Math.random()*feature_stuff.length); document.write(feature_stuff[rand]); </script>
Emmett
October 29, 2007, October 29, 2007 21:02, permalink
There's a lot of repetition in your array, let's get rid of that.
Choosing a random element of an array is easy to break out into a function, and doing so enhances readability.
1 2 3 4 5 6 7 8 9
<script type="text/javascript"> Array.prototype.random = function(){ var rand = Math.floor(this.length * Math.random()); return this[rand]; } var feature_stuff = ['URL 1', 'URL 2', 'URL 3', 'URL 4', 'URL 5']; document.write('<li><a href="#">' + feature_stuff.random() + '</a></li>'); </script>
Fu86
October 30, 2007, October 30, 2007 11:08, permalink
I think you have forgotten the URLs itself
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<script type="text/javascript"> Array.prototype.random = function(){ var rand = Math.floor(this.length * Math.random()); return this[rand]; } var feature_stuff = [ ['URL 1', 'http1'], ['URL 2', 'http2'], ['URL 3', 'http3'], ['URL 4', 'http4'], ['URL 5', 'http5'] ]; var url = feature_stuff.random(); document.write('<li><a href="' + url[1] + '">' + url[0] + '</a></li>'); </script>
Gary Haran
October 30, 2007, October 30, 2007 15:32, permalink
I much prefer Fu86's refactor but honestly this kind of stuff should not be done in Javascript.
Leroy
December 29, 2007, December 29, 2007 14:15, permalink
That's right Gary, make it in php :)
1 2 3 4 5 6 7 8 9 10 11
<?php //array filled with links (text, URL) $feature_stuff = array( array('URL 1', 'http://'), array('URL 2', 'http://'), array('URL 3', 'http://')); //echo random link $rand = rand(0, count($feature_stuff)-1); echo '<li><a href="' . $feature_stuff[$rand][1] . '">' . $feature_stuff[$rand][0] . '</a></li>'; ?>
dbr
April 2, 2008, April 02, 2008 10:28, permalink
Rewrote Loroy's code. It's longer, but nicer to read (function to grab/format lnk, no long string concatenations)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php function randlink($links) { $r_index = rand(0, count($links) - 1); // Get random index within list range list($name,$url) = $links[ $r_index ]; // Grab name and URL of random array element return sprintf('<li><a href="%s">%s</a></li>', $url,$name); // Format a lnk string and return it } //array filled with links (text, URL) $links = array( array('URL 1', 'http://'), array('URL 2', 'http://'), array('URL 3', 'http://') ); //echo random link echo randlink($links); ?>
hubfactor
April 11, 2008, April 11, 2008 21:13, permalink
Back to Javascript...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<script type="text/javascript" id="target"> var urls = [["url1", "name1"], ["url2", "name2"], ["url3", "name3"]]; var link = urls[Math.floor(Math.random() * urls.length)]; var a = document.createElement("a"); a.href = link[0]; a.textContent = link[1]; var li = document.createElement("li"); li.appendChild(a); var target = document.getElementById("target"); target.parentNode.insertBefore(li, target); </script>
Outputs one of the 5 urls randomly.