延迟加载后替换 $(document).ready(function() jquery
replacement for $(document).ready(function() after defer loading jquery
我的页面底部有这种延迟加载脚本。它可以正常加载脚本,但是现在缺少旧的 jquery onload 事件。
var dfLoadStatus = 0;
var dfLoadFiles = [
["//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"],
["//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js",
"/js/jquery.unveil.js",
"/scripts/js/jquery.cookie.js"]
];
function downloadJSAtOnload() {
if (!dfLoadFiles.length) return;
var dfGroup = dfLoadFiles.shift();
dfLoadStatus = 0;
for (var i = 0; i < dfGroup.length; i++) {
dfLoadStatus++;
var element = document.createElement('script');
element.src = dfGroup[i];
element.onload = element.onreadystatechange = function() {
if (!this.readyState || this.readyState == 'complete') {
dfLoadStatus--;
if (dfLoadStatus==0) downloadJSAtOnload();
}
};
document.body.appendChild(element);
}
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
在移动到此延迟加载之前,我曾经在 jquery 加载后使用很多此类脚本:
$(document).ready(function() {
$("img").unveil(200);
});
我如何为那个 jquery 函数创建替换 function/event,它检测何时加载所有这些脚本,或者至少 jquery 已经加载?
我的浏览器正在缓存文件,所以旧代码对我有用。我重写了它,现在它对我来说效果很好。
JavaScript
var dfIndex = 0;
var dfLoadFiles =[
"//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js",
"//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js",
"//cdn.rawgit.com/luis-almeida/unveil/master/jquery.unveil.js",
"//cdn.rawgit.com/carhartl/jquery-cookie/master/src/jquery.cookie.js"];
function downloadJavasSript(){
loadJavaScript(dfLoadFiles[dfIndex]);
}
function loadJavaScript(file,callback){
var head=document.getElementsByTagName("head")[0];
var script=document.createElement('script');
script.src=file;
script.type='text/javascript';
//real browsers
script.onload=isJavaScriptLoaded;
//Internet explorer
script.onreadystatechange = function() {
if (this.readyState == 'complete') {
isJavaScriptLoaded();
}
};
head.appendChild(script);
}
function isJavaScriptLoaded(e){
dfIndex++;
if (dfIndex === dfLoadFiles.length){
pageLoaded();
} else {
loadJavaScript(dfLoadFiles[dfIndex]);
}
}
function pageLoaded(){
console.log('page loaded');
$("img").unveil();
}
if (window.addEventListener)
window.addEventListener("load", downloadJavasSript, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJavasSript);
else window.onload = downloadJavasSript;
我的页面底部有这种延迟加载脚本。它可以正常加载脚本,但是现在缺少旧的 jquery onload 事件。
var dfLoadStatus = 0;
var dfLoadFiles = [
["//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"],
["//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js",
"/js/jquery.unveil.js",
"/scripts/js/jquery.cookie.js"]
];
function downloadJSAtOnload() {
if (!dfLoadFiles.length) return;
var dfGroup = dfLoadFiles.shift();
dfLoadStatus = 0;
for (var i = 0; i < dfGroup.length; i++) {
dfLoadStatus++;
var element = document.createElement('script');
element.src = dfGroup[i];
element.onload = element.onreadystatechange = function() {
if (!this.readyState || this.readyState == 'complete') {
dfLoadStatus--;
if (dfLoadStatus==0) downloadJSAtOnload();
}
};
document.body.appendChild(element);
}
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
在移动到此延迟加载之前,我曾经在 jquery 加载后使用很多此类脚本:
$(document).ready(function() {
$("img").unveil(200);
});
我如何为那个 jquery 函数创建替换 function/event,它检测何时加载所有这些脚本,或者至少 jquery 已经加载?
我的浏览器正在缓存文件,所以旧代码对我有用。我重写了它,现在它对我来说效果很好。
JavaScript
var dfIndex = 0;
var dfLoadFiles =[
"//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js",
"//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js",
"//cdn.rawgit.com/luis-almeida/unveil/master/jquery.unveil.js",
"//cdn.rawgit.com/carhartl/jquery-cookie/master/src/jquery.cookie.js"];
function downloadJavasSript(){
loadJavaScript(dfLoadFiles[dfIndex]);
}
function loadJavaScript(file,callback){
var head=document.getElementsByTagName("head")[0];
var script=document.createElement('script');
script.src=file;
script.type='text/javascript';
//real browsers
script.onload=isJavaScriptLoaded;
//Internet explorer
script.onreadystatechange = function() {
if (this.readyState == 'complete') {
isJavaScriptLoaded();
}
};
head.appendChild(script);
}
function isJavaScriptLoaded(e){
dfIndex++;
if (dfIndex === dfLoadFiles.length){
pageLoaded();
} else {
loadJavaScript(dfLoadFiles[dfIndex]);
}
}
function pageLoaded(){
console.log('page loaded');
$("img").unveil();
}
if (window.addEventListener)
window.addEventListener("load", downloadJavasSript, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJavasSript);
else window.onload = downloadJavasSript;