当前位置: 首页 > 图文教程 > 网络编程 > Javascript > Code:loadScript( )加载js的功能函数

Javascript
在js中使用"with"语句中跨frame的变量引用问题
原型方法的不同写法居然会影响调试的解决方法
在JavaScript中遭遇级联表达式陷阱
JScript内置对象Array中元素的删除方法
获取JavaScript用户自定义类的类名称的代码
使用TextRange获取输入框中光标的位置的代码
使用onbeforeunload属性后的副作用
IE7提供XMLHttpRequest对象为兼容
encode脚本和normal脚本混用的问题与解决方法
用js判断用户浏览器是否是XP SP2的IE6
关于使用runtimeStyle属性问题讨论文章
javascript学习随笔(使用window和frame)的技巧
javascript学习随笔(编写浏览器脚本 Navigator Scripting )
跑马灯效果大全
让超链接显示提示信息的js代码
打开超链需要“确认”对话框的方法
javascript的对话框详解与参数
让网页上的超链接失效,不能点击的js代码
鼠标经过时链接文字的特别震撼的显示效果
显示页面的所有链接的js代码

Javascript 中的 Code:loadScript( )加载js的功能函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 259 ::
收藏到网摘: n/a

复制代码 代码如下:

<script type="text/javascript">
/**
* function loadScript
* Copyright (C) 2006 Dao Gottwald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Contact information:
* Dao Gottwald <dao at design-noir.de>
* Herltestra?e 12
* D-01307, Germany
*
* @version 1.5
* @url http://design-noir.de/webdev/JS/loadScript/
*/
function loadScript (url, callback) {
var script = document.createElement('script');
script.type = 'text/javascript';
/* should be application/javascript
* http://www.rfc-editor.org/rfc/rfc4329.txt
* http://connect.microsoft.com/IE/feedback/ViewFeedback.aspx?FeedbackID=84613
*/
if (callback)
script.onload = script.onreadystatechange = function() {
if (script.readyState && script.readyState != 'loaded' && script.readyState != 'complete')
return;
script.onreadystatechange = script.onload = null;
callback();
};
script.src = url;
document.getElementsByTagName('head')[0].appendChild (script);
}
</script>

实例:
复制代码 代码如下:
<script type="text/javascript">
// prevent google analytics from slowing down page loading
window.addEventListener ('load', function() {
loadScript ('http://www.google-analytics.com/urchin.js', function() {
window._uacct = 'UA-xxxxxx-x';
urchinTracker();
});
}, false);
</script>