Pre-Loading Images

本文介绍如何通过预加载图片及HTML文件利用浏览器缓存来加速网站显示速度。使用JavaScript进行图片预加载,使导航图标等关键元素快速展现;同时讨论了预加载整个页面的可行性和实际操作。
Pre-loading images (and in some cases HTML files) speeds up their display by using your browser's cache. This means quick loading when the files are required.

The Cache

If you're not familiar with the idea of a cache, allow me to explain. Every webpage and its contents you view gets saved in a special part of your hard disk called a cache (pronounced cash). The next time you visit that page the images are taken from the cache instead of being downloaded again. This means they appear faster and make way for new things that may have to be downloaded for the first time. You may have had a run through your 'Temporary Internet Files' folder in Windows — that's Internet Explorer's cache. Netscape keeps its cache in its program folder.
    So, on your first visit you have to download a load of stuff, but on every visit thereafter you're just pulling stuff up from the cache. This is also how it's possible to read websites offline; they're just being read off your hard drive.

The idea of pre-loading images is to load them and put them in the cache before they're even needed. This means that when they are called for they'll appear almost immediately. This property is most important with things like navigation graphics and image rollovers. You can guess what images your reader might need and load them in advance, in the background so they'll never see it happen.

The Script

So, let's get the script. This is done in JavaScript, by the way.

<script type="text/javascript">
<!-- hide from non JavaScript Browsers

Image1= new Image(150,20)
Image1.src="pic1.gif"

Image2 = new Image(10,30)
Image2.src="pic2.gif"

Image3 = new Image(72,125)
Image3.src="pic3.gif"

// End Hiding -->
</script>

That script would pre-load images 1 to 3. If you've used image flips before you can see that that code has a pre-load built into it. If you've never seen one of these run before, here's what it all means:

  • Image1= new Image is the first step of setting up a new script effect for an image
  • (150,20) are the width and height, respectively, of this image
  • Image1.src="pic1.gif" gives the source of the image.

Place the script near the top of your page, in the head if you want. This will ensure it runs early as the page loads.

 

Pre-loading HTML

If you're looking to pre-load HTML files, I'd have to tell you that the JavaScript methods in use are not great. You can see them in » this tutorial. In any case, the concept of pre-loading a whole page seems a bit strange — why have a large extra download on a page when the reader may not need it? If you must pre-load a page, the method I would use is to open a 1x1 iframe on your page with the next page inside it. Clever.


Still using Internet Explorer? This site, and most others, look and perform much better if you’re using a modern browser like Mozilla Firefox instead. Using this browser means you’ll browse the web faster, more securely, with no popups, and you’ll be able to use sites that use the latest CSS and JavaScript. And it’s free! Get a better browser

转载于:https://www.cnblogs.com/kaixin110/archive/2007/08/23/866858.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值