load.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. define([
  2. "../core",
  3. "../core/parseHTML",
  4. "../ajax",
  5. "../traversing",
  6. "../manipulation",
  7. "../selector",
  8. // Optional event/alias dependency
  9. "../event/alias"
  10. ], function( jQuery ) {
  11. // Keep a copy of the old load method
  12. var _load = jQuery.fn.load;
  13. /**
  14. * Load a url into a page
  15. */
  16. jQuery.fn.load = function( url, params, callback ) {
  17. if ( typeof url !== "string" && _load ) {
  18. return _load.apply( this, arguments );
  19. }
  20. var selector, type, response,
  21. self = this,
  22. off = url.indexOf(" ");
  23. if ( off >= 0 ) {
  24. selector = jQuery.trim( url.slice( off ) );
  25. url = url.slice( 0, off );
  26. }
  27. // If it's a function
  28. if ( jQuery.isFunction( params ) ) {
  29. // We assume that it's the callback
  30. callback = params;
  31. params = undefined;
  32. // Otherwise, build a param string
  33. } else if ( params && typeof params === "object" ) {
  34. type = "POST";
  35. }
  36. // If we have elements to modify, make the request
  37. if ( self.length > 0 ) {
  38. jQuery.ajax({
  39. url: url,
  40. // if "type" variable is undefined, then "GET" method will be used
  41. type: type,
  42. dataType: "html",
  43. data: params
  44. }).done(function( responseText ) {
  45. // Save response for use in complete callback
  46. response = arguments;
  47. self.html( selector ?
  48. // If a selector was specified, locate the right elements in a dummy div
  49. // Exclude scripts to avoid IE 'Permission Denied' errors
  50. jQuery("<div>").append( jQuery.parseHTML( responseText ) ).find( selector ) :
  51. // Otherwise use the full result
  52. responseText );
  53. }).complete( callback && function( jqXHR, status ) {
  54. self.each( callback, response || [ jqXHR.responseText, status, jqXHR ] );
  55. });
  56. }
  57. return this;
  58. };
  59. });