Javascript “如何制作”;。加载();忽略某些脚本?
我正在将$(window).Load()上的图像淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入 有没有办法让下面的内容忽略社交脚本Javascript “如何制作”;。加载();忽略某些脚本?,javascript,jquery,Javascript,Jquery,我正在将$(window).Load()上的图像淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入淡入 有没有办法让下面的内容忽略社交脚本 $(window).load(function() { images.fadeIn('slow'); }); 正在加载的社交脚本是显示“共享”按钮所需的脚本,其中包括Facebook、Twitter、Pintere
$(window).load(function() {
images.fadeIn('slow');
});
正在加载的社交脚本是显示“共享”按钮所需的脚本,其中包括Facebook、Twitter、Pinterest、StumbleUpon和Google+。就在几秒钟前,“pinterest”脚本还在加载中,加载它的代码确实启用了“async”,所以我不知道还能做什么?您可以选择两个选项:
1-不要等待window.load
不必等待窗口加载,您可以改为寻找图像加载。例如,在HTML中的图像下方使用此脚本(或使用jQuery的ready
事件):
请注意,我们不依赖于获取load
事件,因为在我们钩住它之前它可能已经触发了。因此,我们在错过所有加载事件的情况下进行初始检查,然后在任何相关图像中看到load
或error
时再次检查
2-异步加载相关脚本
不要在标记中使用
标记,而是通过将script
元素附加到DOM中来添加不想等待的脚本。以这种方式添加的脚本是异步加载的,不会占用窗口。加载事件
例如:
(函数(){
变量脚本=[
"http://example.com/some/script.js",
"http://example.com/some/another_script.js",
"http://example.com/some/script3.js"
};
var指数,脚本;
var parent=document.getElementsByTagName(“脚本”)[0].parentNode;
对于(索引=0;索引
@Cristy:如果img
元素在我们钩住事件之前在DOM中设置了src
(如果我们将钩住事件的代码放在HTML的末尾或就绪的处理程序中,情况就会如此),事件有可能在我们钩住它之前触发。这是一个竞争条件。如果Wild可以在设置src
之前钩住事件,那么我们可以依靠获取事件,但要做到这一点,我们必须手动添加所有img
元素,而不是使用标记。
(function() {
var imgs = $("selector for the images you care about");
var done = false;
// Hook both load and error events on the images
imgs.on("load error", check);
// We may well have missed some, so do a proactive check
check();
function check() {
var count = 0;
if (!done) {
imgs.each(function() {
if (this.complete) {
++count;
}
});
if (count === imgs.length) {
done = true;
imgs.off("load error");
// Do your effect
}
}
}
})();
(function() {
var scripts = [
"http://example.com/some/script.js",
"http://example.com/some/another_script.js",
"http://example.com/some/script3.js"
};
var index, script;
var parent = document.getElementsByTagName("script")[0].parentNode;
for (index = 0; index < scripts.length; ++index) {
script = document.createElement("script");
script.src = scripts[index];
parent.appendChild(script);
}
})();