JavaScript 阻止图像加载
我使用以下代码允许在我的网站上并行下载 JavaScript
var head = document.getElementsByTagName("head")[0];
var sTag1 = document.createElement("script");
sTag1.type = sTag1.type = "text/javascript";
sTag1.src = "http://example.com/one.js";
var sTag2 = document.createElement("script");
sTag2.type = sTag2.type = "text/javascript";
sTag2.src = "http://example.com/two.js";
var sTag1 = document.createElement("script");
sTag3.type = sTag3.type = "text/javascript";
sTag3.src = "http://example.com/three.js";
head.appendChild(sTag1);
head.appendChild(sTag2);
head.appendChild(sTag3);
但是,使用 YSlow,它显示即使 one.js
、two.js
和 Three.js
正在并行下载 - 直到最后一个 JavaScript 完全下载后才会加载图像。
我该怎么做才能让图像不会因 JavaScript 文件下载而被阻止加载。
I'm using the following code to allow parallel JavaScript downloading on my website
var head = document.getElementsByTagName("head")[0];
var sTag1 = document.createElement("script");
sTag1.type = sTag1.type = "text/javascript";
sTag1.src = "http://example.com/one.js";
var sTag2 = document.createElement("script");
sTag2.type = sTag2.type = "text/javascript";
sTag2.src = "http://example.com/two.js";
var sTag1 = document.createElement("script");
sTag3.type = sTag3.type = "text/javascript";
sTag3.src = "http://example.com/three.js";
head.appendChild(sTag1);
head.appendChild(sTag2);
head.appendChild(sTag3);
However, using YSlow, it shows that even though one.js
, two.js
and three.js
are downloading in parallel - images are not loading until the last JavaScript is fully downloaded.
What can I do to allow images to not be blocked from loaded due to my JavaScript files downloading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
标记上方加载 Javascript 文件。
Load your Javascript files right above the
</body>
tag.你从哪里触发该代码?因为您可以等待执行引用的代码,直到看到
window.load
事件,例如:在加载所有图像之前,不会触发
window.load
事件,这样您就可以确保脚本不会妨碍您。当然,这也为用户留下了相当大的时间来开始使用页面进行操作,因此您需要确保页面不需要 JavaScript 来运行。Where are you triggering that code from? Because you could wait to execute your quoted code until you see the
window.load
event, e.g.:The
window.load
event isn't fired until all of the images are loaded, so you'll be sure the scripts aren't getting in the way. Of course, it also leaves quite a large margin of time for the user to start doing things with the page, so you need to be sure the page doesn't need that JavaScript to be functional.