全部展开 | 全部折叠 | 速查表

返回值:XMLHttpRequestjQuery.getScript(url, [callback])

概述

通过 HTTP GET 请求载入并执行一个 JavaScript 文件。

jQuery 1.2 版本之前,getScript 只能调用同域 JS 文件。 1.2中,您可以跨域调用 JavaScript 文件。注意:Safari 2 或更早的版本不能在全局作用域中同步执行脚本。如果通过 getScript 加入脚本,请加入延时函数。

参数

url,[callback]String,FunctionV1.0

url:待载入 JS 文件地址。

callback:成功载入后回调函数。

示例

描述:

载入 <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.");
});