锁定老帖子 主题:哈哈找到一个能动态加载js的方法
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-04-18
最后修改:2011-04-18
//加载新的js function _GetJsData(url, callback) { var scripts = document.createElement("script"); document.body.appendChild(scripts); scripts.onload = function() { callback(); document.body.removeChild(this); }; scripts.onreadystatechange = function() { if (this.readyState == "loaded") { callback(); document.body.removeChild(this); } }; scripts.charset = "GBK"; scripts.src = url; } _GetJsData('http://www.uefirst.com/index.js',isok); var isok=function(){alert('isok')}; http://www.uefirst.com/bbs/read-htm-tid-22.html 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-04-18
同一域下的js,还有一个方法
XHR 获取js文本,然后eval~ 这样是同步的。~ 不需要callback |
|
返回顶楼 | |
发表时间:2011-04-18
632244434 写道 同一域下的js,还有一个方法
XHR 获取js文本,然后eval~ 这样是同步的。~ 不需要callback 我是需要callback的,比如url是个json数据 但是你的方法来段代码看看呗.... |
|
返回顶楼 | |
发表时间:2011-04-18
|
|
返回顶楼 | |
发表时间:2011-04-21
|
|
返回顶楼 | |
发表时间:2011-04-22
这种onload是dom规范里面的吗?浏览器支持情况如何啊??
|
|
返回顶楼 | |
发表时间:2011-04-22
http://images.39.net/js/google/google_730_ask.js 似乎这个没办法加载
|
|
返回顶楼 | |
发表时间:2011-04-23
jquery、prototype 等之类的框架几年前就已经有了...~ 去看看源码吧...
|
|
返回顶楼 | |
发表时间:2011-04-24
foxtool23 写道 632244434 写道 同一域下的js,还有一个方法
XHR 获取js文本,然后eval~ 这样是同步的。~ 不需要callback 我是需要callback的,比如url是个json数据 但是你的方法来段代码看看呗.... callback就直接是载入的JS文本啦,可以看看高性能Javascript这本书 |
|
返回顶楼 | |
发表时间:2011-04-24
最后修改:2011-04-24
Jquery 1.4源码:
var head = document.getElementsByTagName("head")[0] || document.documentElement; var script = document.createElement("script"); script.src = s.url; // Handle Script loading // Attach handlers for all browsers script.onload = script.onreadystatechange = function() { if ( !done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") ) { done = true; jQuery.handleSuccess( s, xhr, status, data ); jQuery.handleComplete( s, xhr, status, data ); // Handle memory leak in IE script.onload = script.onreadystatechange = null; if ( head && script.parentNode ) { head.removeChild( script ); } } }; } // Use insertBefore instead of appendChild to circumvent an IE6 bug. // This arises when a base node is used (#2709 and #4378). head.insertBefore( script, head.firstChild ); // We handle everything using the script element injection return undefined; |
|
返回顶楼 | |