加载中...

jQuery.getScript()


概述    jQuery.getScript( url [, success(script, textStatus, jqXHR) ] )

返回值:XMLHttpRequest

描述: 使用一个HTTP GET请求从服务器加载并执行一个 JavaScript 文件

  • V : 1.0jQuery.getScript( url [, success(script, textStatus, jqXHR) ] )

    • url
      类型: String
      一个包含发送请求的URL字符串。
    • success(script, textStatus, jqXHR)
      类型: Function()
      当请求成功后执行的回调函数。

这是一个Ajax函数的缩写,这相当于:

  1. $.ajax({
  2. url: url,
  3. dataType: "script",
  4. success: success
  5. });

这个脚本在全局环境中执行,所以指向其他变量和运行jQuery函数。被加载的脚本同样作用于当前页面:

Success Callback(成功回调)

一旦脚本已经被加载,将触发回调  但不一定执行。

  1. $(".result").html("<p>Lorem ipsum dolor sit amet.</p>");

通过引用这个文件名,脚本被包含进来并执行:

  1. $.getScript("ajax/test.js", function(data, textStatus, jqxhr) {
  2. console.log(data); //data returned
  3. console.log(textStatus); //success
  4. console.log(jqxhr.status); //200
  5. console.log('Load was performed.');
  6. });

Handling Errors(错误处理)

从jQuery 1.5开始,你可以用.fail()来处理错误:

  1. $.getScript("ajax/test.js")
  2. .done(function(script, textStatus) {
  3. console.log( textStatus );
  4. })
  5. .fail(function(jqxhr, settings, exception) {
  6. $( "div.log" ).text( "Triggered ajaxError handler." );
  7. });

jQuery1.5之前,不得不使用全局的.ajaxError()回调事件来处理$.getScript()错误:

  1. $( "div.log" ).ajaxError(function(e, jqxhr, settings, exception) {
  2. if (settings.dataType=='script') {
  3. $(this).text( "Triggered ajaxError handler." );
  4. }
  5. });

Caching Responses(缓存响应)

默认情况下,$.getScript() cache选项被设置为 false。这将在请求的URL的查询字符串中追加一个时间戳参数,以确保每次浏览器下载的脚本被重新请求。您可以全局的使用 $.ajaxSetup()设置cache(缓存)属性覆盖该功能:

  1. $.ajaxSetup({
  2. cache: true
  3. });

另外,  你可以使用更灵活的 $.ajax() 方法定义一个新的方法

示例

实例

载入<a title="http://jquery.com/plugins/project/color" class="external text" href="http://jquery.com/plugins/project/color">jQuery 官方颜色动画插件</a> 成功后绑定颜色变化动画。

HTML 代码:
  1. <button id="go">» Run</button>
  2. <div class="block"></div>
jQuery 代码:
  1. jQuery.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js", function(){
  2. $("#go").click(function(){
  3. $(".block").animate( { backgroundColor: 'pink' }, 1000)
  4. .animate( { backgroundColor: 'blue' }, 1000);
  5. });
  6. });

实例

加载并执行 test.js。

jQuery 代码:
  1. $.getScript("test.js");

实例

加载并执行 test.js ,成功后显示信息。

jQuery 代码:
  1. $.getScript("test.js", function(){
  2. alert("Script loaded and executed.");
  3. });

实例

定义了一个$.cachedScript()方法可以获取缓存的脚本

  1. jQuery.cachedScript = function(url, options) {
  2. // allow user to set any option except for dataType, cache, and url
  3. options = $.extend(options || {}, {
  4. dataType: "script",
  5. cache: true,
  6. url: url
  7. });
  8. // Use $.ajax() since it is more flexible than $.getScript
  9. // Return the jqXHR object so we can chain callbacks
  10. return jQuery.ajax(options);
  11. };
  12. // Usage
  13. $.cachedScript("ajax/test.js").done(function(script, textStatus) {
  14. console.log( textStatus );
  15. });

实例

我们动态加载新的官方jQuery 颜色动画插件,一旦该插件加载完成就会触发一些颜色动画。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery.getScript demo</title>
  6. <style>
  7. .block {
  8. background-color: blue;
  9. width: 150px;
  10. height: 70px;
  11. margin: 10px;
  12. }
  13. </style>
  14. <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  15. </head>
  16. <body>
  17. <button id="go">&raquo; Run</button>
  18. <div class="block"></div>
  19. <script>
  20. var url = "//code.jquery.com/color/jquery.color-git.js";
  21. $.getScript( url, function() {
  22. $( "#go" ).click(function() {
  23. $( ".block" )
  24. .animate({
  25. backgroundColor: "rgb(255, 180, 180)"
  26. }, 1000 )
  27. .delay( 500 )
  28. .animate({
  29. backgroundColor: "olive"
  30. }, 1000 )
  31. .delay( 500 )
  32. .animate({
  33. backgroundColor: "#00f"
  34. }, 1000 );
  35. });
  36. });
  37. </script>
  38. </body>
  39. </html>
运行一下


还没有评论.