Skip to content

Instantly share code, notes, and snippets.

@kohashi
Created March 7, 2012 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohashi/1992298 to your computer and use it in GitHub Desktop.
Save kohashi/1992298 to your computer and use it in GitHub Desktop.
preload_images
<?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