这是在大容量站点中经过测试的解决方案。
首先,为了避免登录页面资源和带宽的预加载资源之间的竞争,您可以使用 javascript 延迟加载:
var prevOnLoad=window.onload;
function onLoadPreloadComponents() {
if(prevOnLoad) {
try{
prevOnLoad();
}catch(err){
}
}
preloadSiteComponents();
}
window.onload=onLoadPreloadComponents;
这不是我解决这个问题的方法,因为在我的用例中,最终加载着陆时会发出一个 Flash 事件(使用 Flash 到 JS 桥)信号。但是前面的代码也必须有效。当浏览器触发加载页面事件时,此函数将执行先前的 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>
您可以在这里看到有效的解决方案。
使用 Firebug 查看此组件的延迟加载。