MediaWiki:Common.js/shuffle.js
Jump to navigation
Jump to search
Note: After saving, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/**
* Shuffle objects around for fairness. Searches for a DOM
* branch like this:
* <div class="shuffle">
* <div class="shuffle-item">One</div>
* <div class="shuffle-item">One</div>
* ...
* </div>
* Any content that's not in a "shuffle-item" tag will end up
* at the top of the "shuffle" element.
* Maintainers: Happy-melon
*/
if( getElementsByClassName(document, "*", "shuffle").length > 0 ){ addOnloadHook( function() {
var area = getElementsByClassName(document, "*", "shuffle")[0];
var divs = getElementsByClassName( area, '*', 'shuffle-item' );
var rand = [];
for ( var i=0; i<divs.length; i++ ) {
rand[i] = Math.random();
}
var sorted = [];
//Insertion sort
for( var i=0; i<divs.length; i++ ){
var n = 0;
var x = 99;
for( var j=0; j<divs.length; j++ ){
if( rand[j] < x ){
n = j;
x = rand[j];
}
}
sorted[i] = divs[n];
rand[n] = 100;
}
for( var i=0; i<sorted.length; i++ ){
area.appendChild( sorted[i] );
}
})}