这是一个在大容量网站中经过测试的解决方案。
首先,为避免登录页面资源与带宽的预加载资源之间的竞争,您可以使用 javascript 延迟加载:
var prevOnLoad=window.onload;
function onLoadPreloadComponents() {
if(prevOnLoad) {
try{
prevOnLoad();
}catch(err){
}
}
preloadSiteComponents();
}
window.onload=onLoadPreloadComponents;
这不是我解决此问题的方法,因为在我的用例中,最终加载着陆时会发出一个 Flash 事件(使用 Flash 到 JS 桥接)信号。但是前面的代码也必须有效。当浏览器触发 load page 事件时,此函数将执行之前的 onLoad 代码和预加载。
我在要加载 iframe 的位置放置了一个空的 div 硬币容器。
<div id="mainSiteComponentsContainer" style="display: none;">
</div>
而功能码是:
function preloadSiteComponents() {
try{
document.getElementById('mainSiteComponentsContainer')
.innerHTML=
"<iframe src=\"http://www-components.renxo-cdn.net/preload-site-components-data-url-scheme-version-1.2.84-css-1.0.53.html\" frameborder=\"no\" height=\"0px\" width=\"0px\"></iframe>";
}catch(err) {
}
}
如您所见,iframe 的链接 url 是动态的,它会在不同的平台版本(不同的部署)之间变化,以避免在新部署中出现不需要的浏览器缓存。
iframe 中的 HTML 可能是这样的(例如):
<html class=" gecko win js" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="noindex,nofollow" name="robots">
<script src="http://www-components.renxo-cdn.net/scripts/lib-1.2.84.js" type="text/javascript">
<link href="http://www-components.renxo-cdn.net/styles/skin-data-url-scheme-1.0.53.css" media="all" type="text/css" rel="stylesheet">
</head>
<body> </body>
</html>
在这里您可以看到指向我要预加载的组件的链接。
最后,div 硬币容器将拥有 iframe。 onLoad 事件后:
<div id="mainSiteComponentsContainer" style="display: none;">
<iframe width="0px" height="0px" frameborder="no" src="http://www-components.renxo-cdn.net/preload-site-components-data-url-scheme-version-1.2.84-css-1.0.53.html">
<html class=" gecko win js" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="noindex,nofollow" name="robots">
<script src="http://www-components.renxo-cdn.net/scripts/lib-1.2.84.js" type="text/javascript">
<link href="http://www-components.renxo-cdn.net/styles/skin-data-url-scheme-1.0.53.css" media="all" type="text/css" rel="stylesheet">
</head>
<body> </body>
</html>
</iframe>
</div>
您可以看到有效的解决方案here。
使用 Firebug 查看此组件的延迟加载。