JavaScript 阻止图像加载

发布于 2024-08-29 13:11:17 字数 774 浏览 8 评论 0原文

我使用以下代码允许在我的网站上并行下载 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.jstwo.jsThree.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎的声音 2024-09-05 13:11:17

标记上方加载 Javascript 文件。

Load your Javascript files right above the </body> tag.

此生挚爱伱 2024-09-05 13:11:17

你从哪里触发该代码?因为您可以等待执行引用的代码,直到看到 window.load 事件,例如:

<script type='text/javascript'>
function loadMyScripts() {
    /* ...your loading code here...*/
}
window.onload = loadMyScripts; // Or use addEventListener/attachEvent to do it
</script>

在加载所有图像之前,不会触发 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.:

<script type='text/javascript'>
function loadMyScripts() {
    /* ...your loading code here...*/
}
window.onload = loadMyScripts; // Or use addEventListener/attachEvent to do it
</script>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文