Forum:Random function

Hello, on my wiki I recently add a new script, this: /*  Da http://nonciclopedia.wikia.com/  */ function randomlist { if (document.getElementsByClass('randomlist') &&    document.getElementsByClass('randomcontent') &&     document.getElementsByClass('randomtext')) { var listNumber = document.getElementsByClass('randomlist').length; for (var k = 0; k < listNumber; k++) { var max = parseInt(document.getElementsByClass('randomlist')[k].title); var List = document.getElementsByClass('randomcontent')[k].getElementsByTagName('li'); var Text = document.getElementsByClass('randomtext')[k]; var N = new Array; Text.innerHTML = ''; for (var i = 0; i < max; i++) { do { n = Math.round(Math.random * (List.length - 1)); for (var j = 0; j < i && n != -1; j++) { if (N[j] == n) n = -1; }   } while (n == -1) N[i] = n;   var Item = document.createElement('div'); Item.innerHTML = List[n].innerHTML; Text.appendChild(Item); } } } } addOnloadHook(randomlist); which works along with this template:  • ...

The purpose of the scripts is to pick a random sentence out from a dot list (those starting with *), the list can be a transcluded page. It was slighlty modified from the original but nothing essential. Now, the function works fine, but I need to use it with a list of template more then simple sentences. If the tamplate contains wikitables, there can be problems, but with html tables seems there are lesser. I found a way for using it by using this code in the page-list: *

Now is there a way to made the function more "versatile", for example by using a code like  to tag the elements and not the dot list?

As it is now, is acceptable, but there are sometimes errors if I use template. You can see an application here: the quote should be a random quote from another page, and the "question box" picks 4 questions out from another page. If someone can find a better solution I will appreciated it, but otherwise no problems.


 * Most wikis use the RandomSelection extension, which is enabled by default on most Wikia wikis, to randomize quotes, featured content or polls. Your wiki also uses it for thr main page images of the right. I'd say it's more versatile and the syntax is more intuitve than using javascript for that. DPL is also very useful for deriving parts of pages and randomizing them if you now how to use it.


 * The little javascript experience I have tells me, that if you want to make the function more versatile, you would need to include more options in the query and adjust the randomizer accordingly. Currently it only looks for  elements which are list items. You could also put the objects you want to randomize within   but I don't know if this would work bug-free. — Sovq 20:37, June 26, 2011 (UTC)