Load random CSS on page refresh Load random CSS on page refresh apache apache

Load random CSS on page refresh


var link = [];link[0] = "http://site.com/css/style1.css";link[1] = "http://site.com/css/style2.css";link[2] = "http://site.com/css/style3.css";$(function() {    var style = link[Math.floor(Math.random() * link.length )];    $('<link />',{        rel :'stylesheet',        type:'text/css',        href: style    }).appendTo('head');});

Edit : Thank you Basil Siddiqui!

var link = [];link[0] = "http://site.com/css/style1.css";link[1] = "http://site.com/css/style2.css";link[2] = "http://site.com/css/style3.css";$(function() {    var style = link[Math.floor(Math.random() * link.length )];    if (document.createStyleSheet){        document.createStyleSheet(style);    }else{        $('<link />',{            rel :'stylesheet',            type:'text/css',            href: style        }).appendTo('head');    }});


If you're using PHP, you can read your CSS directory and pick a random file like this:

<?php$css_dir = '/css';$files   = array();foreach(glob($cssdir.'/*.css') as $file) {    $array[] = $file;}echo '<link rel="stylesheet" type="text/css" href="' . array_rand($files, 1) . '">';?>


Thanks for your advice, didn't realise it was possible with a simple line of php and actually found that this method was pretty short and sweet

<link href="/styles/<?php echo mt_rand(1, 5); ?>.css" rel="stylesheet" type="text/css"/>

Found it here, http://forum.mamboserver.com/showthread.php?t=61029

Many thanks

ps. A List Apart also have a pretty simple and brilliant way to switch images, http://www.alistapart.com/articles/randomizer/