/    Sign up×
Community /Pin to ProfileBookmark

Preloaded Images

Hello there,

I use javascript to preload images as show below. The problem is that every time a page is refreshed, it preloads again. Is there any way to get an image into memory that stays there forever without any more preloading?

<script type=’text/javascript’>
if (document.images) {
img001 = new Image(); img001.src = ‘images/background.jpg ‘;
img002 = new Image(); img002.src = ‘images/banner.jpg ‘;
}
</script>

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperJan 10.2010 — This is my understanding of it:

When the page refreshes, the browser will by default pull any locally cached images (from a previous page load) unless the developer told the browser not to locally cache on that prior page using meta tags. When pre-loading via Javascript you're actually creating a new image object which forces the browser to download the image and put it into the local cache (overwriting any existing of the same) before the rest of the page is processed. Even if it doesn't overwrite and returns the cached copy, it still downloads because initiating a new image object does that. There is also the additional overhead of the Javascript code itself executing client side - that's not cached at all - every page is unique to the browser in terms of client side code.

If I am wrong on this, others will chime in and correct me.

But one thing I know - there is no such thing as "forever" for caching - even the user can set their browser to empty the cache upon exit, or manually, or automatically as a FIFO mechanism based on the amount of allocated cache space and memory on the client side. As a developer you have limited controls on this stuff.

-jim
Copy linkTweet thisAlerts:
@NogDogJan 10.2010 — You can load them into a hidden HTML element:
[code=php]
<?php
$images = array('pea****.png', 'middle_earth.jpg', 'kindle_skin.jpg', 'prototype.png');
$randImg = $images[array_rand($images)];
?>
<html><head><title>Preload Test</title>
<style type='text/css'>
#test {
display:none;
}
</style>
<script type="text/javascript">
function preload()
{
document.getElementById('test').innerHTML = "
<?php
foreach($images as $img)
{
echo "<img src='http://charles-reace.com/Photos/kindle/$img' />\
";
}
?>";
}
</script>
</head>
<!-- use onload event so preloading starts after rest of page is displayed -->
<body onload="preload();">
<p><img src="http://charles-reace.com/Photos/kindle/<?php echo $randImg; ?>" /></p>
<!-- this is where we'll preload the images -->
<div id='test' style='display:hidden;'></div>
</body>
</html>
[/code]


Heh...I like the fact that this forum censors the male version of a peahen. :rolleyes:
Copy linkTweet thisAlerts:
@MindzaiJan 11.2010 — This is my understanding of it:

When the page refreshes, the browser will by default pull any locally cached images (from a previous page load) unless the developer told the browser not to locally cache on that prior page using meta tags. When pre-loading via Javascript you're actually creating a new image object which forces the browser to download the image and put it into the local cache (overwriting any existing of the same) before the rest of the page is processed. Even if it doesn't overwrite and returns the cached copy, it still downloads because initiating a new image object does that. There is also the additional overhead of the Javascript code itself executing client side - that's not cached at all - every page is unique to the browser in terms of client side code.
[/QUOTE]


I think chrome and IE behave a bit differently - ie they will still read from cache when a new element is created. I haven't tested this extensively but they certainly don't fire the load event, whilst firefox (for example) does. I'd be happy to hear if this is a mistake on my part as it was giving me headaches last week!

@OP - if you take NogDog's advice, be sure to set a future expires header for the images to get the full benefit of the cache. Without this the browser will still send an HTTP request to check for the 304 Not Modified header, even if the image is not re-downloaded.
Copy linkTweet thisAlerts:
@SrWebDeveloperJan 11.2010 — Thanks for checking on chrome and MSIE, Mindzai, as to the caching thing. I guess it's safe to say there is no "universal" browser behavior as to that. And thanks for adding the part about the 304 header too.
×

Success!

Help @tonyeveland spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...