Skip to content. Skip to navigation

WDIL.org

Personal tools
Document Actions

Randomizer javascript menu

by Adrian Van Allen last modified 2006-09-14 16:16

Here's a fairly simple javascript menu that randomizes the contents of the list on "refresh" of the browser. There are two pieces: an external randomizer.js script and an HTML page that calls to it.

here's the javascript:

/* list all items */
var a_items = [
    'question1',
    'question2',
    'question3',
    'question4',
    'question5',
    'question6',
    'question7',
    'question8',
    'question9',
    'question10'

];

function randomize (a_items, n_count) {
    var n_index, s_html = '<table cellpadding="4" cellspacing="1" border="0"><tr>';
    while (a_items.length && n_count) {
        n_index = Math.ceil(Math.random() * a_items.length) - 1;
        s_html += '<td>' + a_items[n_index] + '</td>';
        a_items[n_index] = a_items[a_items.length - 1];
        a_items.length = a_items.length - 1;
        n_count--;
    }
    return s_html + '</tr></table>';
}

// call randomizer
// param 1 - list of items
// param 2 - number of items to display
document.write(randomize (a_items, 10));

and here's the call to it to put in the HTML page:

<html>
<body>

<script language="javascript" src="randomize.js"></script>

</body>
</html>


javascipt randomizer

Posted by john alexander at 2011-04-05 03:03
Thanks, for the javascript. it is very helpful in understanding the external javascript.js and html page required to call the browser contents.

John Alex,

<a href="http://www.standmixer-reviews.com">Stand Mixer Reviews</a>