为其他页面预加载外部文件(CSS,JavaScript) [英] Pre-loading external files (CSS, JavaScript) for other pages

查看:148
本文介绍了为其他页面预加载外部文件(CSS,JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,如果有一个有效的方法等待网站的首页加载,然后预加载CSS和脚本文件,我知道可能需要为网站上的其他网页。 / p>

我希望网站的首页尽可能快(精简和平均)。用户可能不会立即点击链接。由于可能会有一些空闲时间,这似乎是预先加载一些外部资产的适当时间。预加载应该使它们被缓存。当用户点击另一个页面时,所需的唯一请求将是内容和可能的一些图像等。



有没有人这样做?
这是一个坏主意吗?
有一个优雅的方式来实现它吗?

解决方案

这是一个有趣的想法,我不知道可以通过Javascript(至少是CSS部分)完成。



您可以在页面上放置一个IFRAME,其中包含您想加载的其他资源,它隐藏... ...

  .preload {
position:absolute;
top:-9999px;
height:1px;
width:1px;
overflow:hidden;
}

如果你把这放在页面的末尾,在网页的其余部分之后加载 。如果没有,则可以使用一些Javascript和setTimeout在网页加载后实际创建IFRAME。


I'm curious if there is an efficient way to wait for the front page of a site to load, and then pre-load CSS and script files that I know will likely be needed for the other pages on the site.

I want the front page of the site to be as fast as possible (lean and mean). It's likely that the user will not immediately click on a link. Since there will likely be some idle time, this seems like an opportune time to pre-load some of the external assets. Pre-loading should cause them to become cached. When the user does click on another page, the only request needed will be for the content and possibly some images, etc.

Has anyone done this? Is it a bad idea? Is there an elegant way to implement it?

解决方案

That's an interesting idea, I'm not sure if it could be done via Javascript (at least the CSS part).

You might place an IFRAME on the page with the other resources you want to load, but put some CSS on it to hide it...

.preload {
    position : absolute;
    top      : -9999px;
    height   : 1px;
    width    : 1px;
    overflow : hidden;
}

If you put this at the end of the page then I would think it would load after the rest of the page. If not then you could use some Javascript and setTimeout to actually create the IFRAME after the page loads.

这篇关于为其他页面预加载外部文件(CSS,JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆