Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- random choice from array of functions


Fotiman - 12:42 pm on Mar 21, 2011 (gmt 0)


Try this:

function doPage() {
var pages = [page1, page2, page3],
randomnumber = Math.floor(Math.random() * pages.length);
pages[randomnumber]();
}

In that example, the pages array contains references to functions page1, page2, and page3. So doing:

pages[randomenumber]

is referring to one of those function references, and the () at the end will cause the function to execute.

Side note, avoid using "new Array()". Instead, just use the square bracket notation to create the array. This avoids the overhead of calling "new" and is slightly more compact. :)


Thread source:: http://www.webmasterworld.com/javascript/4284932.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com