$.getScript(url,[callback])
返回值
XMLHttpRequest
概述
通过 HTTP GET 请求载入并执行一个 JavaScript 文件。
jQuery 1.2 版本之前,getScript 只能调用同域 JS 文件。 1.2中,您可以跨域调用 JavaScript 文件。注意:Safari 2 或更早的版本不能在全局作用域中同步执行脚本。如果通过 getScript 加入脚本,请加入延时函数。
参数
示例
示例一
描述: 载入 <a title="http://jquery.com/plugins/project/color" class="external text" href="http://jquery.com/plugins/project/color"> jQuery 官方颜色动画插件</a> 成功后绑定颜色变化动画。
HTML代码
<button id="go">» Run</button>
<div class="block"></div>
jQuery 代码
jQuery.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js", function(){
$("#go").click(function(){
$(".block").animate( { backgroundColor: 'pink' }, 1000)
.animate( { backgroundColor: 'blue' }, 1000);
});
});
示例二
描述: 加载并执行 test.js。
jQuery代码
$.getScript("test.js");
示例三
描述:加载并执行 test.js ,成功后显示信息。
jQuery代码
$.getScript("test.js", function(){
alert("Script loaded and executed.");
});