I wrote a php script to stick in my page just before my main stylesheet.
The idea is to split all the background images needed by my page across a few different subdomains so that I can use more than two parallel HTTP requests (latency is horrible from Australia to American hosting).
I know that this requires resolving extra DNS lookups, but the yahoo speed guide recommends using 2-4 domains.
This is what I came up with, I was wondering if anyone could come up with something shorter?
(the base_url() function is part of the codeigniter framework I'm using, it just returns the url of the site straight out of a config file)
Specifically, is there a better way to insert the subdomain into the $str variable than using substr_replace()?
is there a better way to distribute the elements amongst the three domains I'm using (nothing, www., media.)? Looping through the foreach and switching just doesn't seem like it would be the most efficient way, and there's no way to guarantee that you even utilize all three domains every time with the random approach (although statistically it should get better with more complex sites)
<style type='text/css'>
<?php
//syntax for each item: 'css-selector'=>'image-filename',
$image_list= array(
'.someclass'=>'someImage.png'
);
function str_r($sub,$css,$src){
$dom='';
$str='';
$dom=substr_replace(base_url(),$sub,7,0);
$str=$css.'{background-image:url(\''.$dom.'images/'.$src.'\');}'."\n";
return $str;
}
foreach($image_list as $css=>$src){
$mix = floor(rand(0,3));
switch($mix){
case 0:
echo str_r('',$css,$src);
break;
case 1:
echo str_r('www.',$css,$src);
break;
case 2:
echo str_r('media.',$css,$src);
break;
default:
echo str_r('',$css,$src);
}
}
?>
</style>