Created
March 7, 2012 09:52
-
-
Save kohashi/1992298 to your computer and use it in GitHub Desktop.
preload_images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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