22智能在线
新记
javascript或jquery如何实现延迟加载js文件
如题所述
举报该文章
相关建议 2015-09-20
1、把js的代码放在body里面,最好是放在页面最后的位置,而不是放在head里面。这样整个页面加载结束的时候才会加载到js。
2、你可以使用js函数动态的修改页面,加载js。下面附上两个函数,可以用来加载js或者css。
/*Title : 包含其它 js 文件。
* Description : 将其它 Js 文件引入本文件中,方便管理、维护。*/
function include_js(path) {
var sobj = document.createElement('script');
sobj.type = "text/javascript";
sobj.src = path;
var headobj = document.getElementsByTagName('head')[0];
headobj.appendChild(sobj);
}
/* Title : 包含其它 css 文件。
* Description : 将其它 css 文件引入本文件中,方便管理、维护。 */
function include_css(path) {
var fileref = document.createElement("link")
fileref.rel = "stylesheet";
fileref.type = "text/css";
fileref.href = path;
}
3、利用ajax加载js。
温馨提示:内容为网友见解,仅供参考
当前网址:
https://22.t2y.org/zz/2sictihts.html
其他看法
第1个回答 2013-08-23
// 请看jquery的getScript
// 想加载js的时候
$.getScript('url', callback);
// 如果指定多少时间之后加载js
setTimeout(function() {
$.getScript('url', callback);
}, 1000);
本回答被网友采纳
第2个回答 2013-08-23
$(function(){
//要加载JS文件的路径先不填写
setTimeote("这里取到要加载JS文件的路径","延迟时间");
//在这里 用JS 给SRC赋值
})
第3个回答 2013-08-22
setTimeout
相似回答
大家正在搜
相关问题
网页如何延时引用JS文件,如何延迟加载JS文件
怎么让html里的js脚本延迟5秒运行?
js jquery 延迟加载 代码
如何在单独的js文件里调用jquery
怎样使用lazyload延时加载js和css
js延迟加载的方式有哪些?
section 怎么延迟加载js
如何使用jquery动态加载js,css文件