Skip to content

Instantly share code, notes, and snippets.

@juno
Created July 26, 2008 02:12
Show Gist options
  • Save juno/2577 to your computer and use it in GitHub Desktop.
Save juno/2577 to your computer and use it in GitHub Desktop.
Image preloading helper for symfony.
<?php
/**
* Returns a html to preload images using jQuery.
*
* @param array $urls An array of image URLs
* @return string HTML contents
*/
function preload_images($urls)
{
if (!is_array($urls)) {
$urls = array($urls);
}
$lines = array();
$lines[] = '<script type="text/javascript">';
$lines[] = '//<![CDATA[';
foreach ($urls as $url) {
$lines[] = "jQuery('<img>').attr('src', '{$url}');";
}
$lines[] = '//]]>';
$lines[] = '</script>';
return implode("\n", $lines);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment