且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在index.html中预加载图像以在其他页面上使用

更新时间:2023-12-03 09:02:58

回答:

原来,如果我只是将上述js放在index.html的底部,它会将图像加载到缓存中,然后当用户单击以转到另一个页面时,浏览器将抓取该图像包含图像。

换句话说,我已经有了答案!

I have a large image that displays as a background on another page of my website. I want to preload this image when users first go to my webpage so that when they eventually move to the page with the image on it, it loads instantly.

I use this to preload:

<script type="text/javascript">
  if (document.images) {
  img1 = new Image();
  img1.src = "image.jpg";
}
</script>

How do I use this so it stores the image to use on a later page?

Thanks in advance!

ANSWER:

Turns out if I simply put the above js at the bottom of my index.html, it loads the image into the cache and then the browser will grab that image when the user clicks to go to another page that includes the image.

In other words, I already had the answer!