421453710d51d7daaea8069af0aa4126

Outputs one of the 5 urls randomly.

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 !

Avatar

Jon

October 29, 2007, October 29, 2007 18:25, permalink

2 ratings. Login to rate!
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>
Avatar

Emmett

October 29, 2007, October 29, 2007 21:02, permalink

2 ratings. Login to rate!

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>
Ee0505bbd355292778077fb662c88f13

Fu86

October 30, 2007, October 30, 2007 11:08, permalink

2 ratings. Login to rate!

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>
403e57e2be130d2218f992b86dfa8260

Gary Haran

October 30, 2007, October 30, 2007 15:32, permalink

No rating. Login to rate!

I much prefer Fu86's refactor but honestly this kind of stuff should not be done in Javascript.

Avatar

Leroy

December 29, 2007, December 29, 2007 14:15, permalink

1 rating. Login to rate!

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>';
?>
Efdad317c297a2aee89ef435995dbdb6

dbr

April 2, 2008, April 02, 2008 10:28, permalink

No rating. Login to rate!

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);
?>
Ed9c50a6db8b5e078b5ef84306a8477c

hubfactor

April 11, 2008, April 11, 2008 21:13, permalink

No rating. Login to rate!

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>

Your refactoring





Format Copy from initial code

or Cancel