延迟第三方JavaScript的加载 [英] delay the loading of 3rd party javascript

查看:92
本文介绍了延迟第三方JavaScript的加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法延迟加载第三方JavaScript文件,直到页面的其余部分完成加载?

解决方案

您可以附加到页面的onload事件,一旦触发,您可以动态地插入对文件的引用。例如:

 函数loaded(){
var
$ b

el = document.createElement(script);
el.src =somefile.js;
el.type =text / javascript;
document.getElementsByTagName(head)[0] .appendChild(el);
}


Is there ANY method to delay the loading of 3rd party JavaScript files until the rest of the page has finished loading?

解决方案

You can attach to the onload event of page and once that fires you can dynamically insert the references to the files.

For example:

function loaded(){
   var el = document.createElement("script");
   el.src = "somefile.js";
   el.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(el);
}

这篇关于延迟第三方JavaScript的加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆