X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fajax%2Fajax.js;h=dd54d0c30f7c455399ee2731f5c816312848e40a;hb=f28f199dc0a353135ef8b9afa2f3d25c6ffd2c75;hp=be9c32006280d22fd8729090fca3e700f057b8bd;hpb=a42a8e5afa2e4ca7690081bce9899d5e6e0d1cf4;p=jquery.git diff --git a/src/ajax/ajax.js b/src/ajax/ajax.js index be9c320..dd54d0c 100644 --- a/src/ajax/ajax.js +++ b/src/ajax/ajax.js @@ -1,227 +1,444 @@ -// AJAX Plugin -// Docs Here: -// http://jquery.com/docs/ajax/ - -/** - * Load HTML from a remote file and inject it into the DOM - */ -jQuery.fn.load = function( url, params, callback ) { - // I overwrote the event plugin's .load - // this won't happen again, I hope -John - if ( url && url.constructor == Function ) - return this.bind("load", url); - - // Default to a GET request - var type = "GET"; - - // If the second parameter was provided - if ( params ) { - // If it's a function - if ( params.constructor == Function ) { - // We assume that it's the callback - callback = params; - params = null; - - // Otherwise, build a param string - } else { - params = jQuery.param( params ); - type = "POST"; +jQuery.fn.extend({ + load: function( url, params, callback ) { + if ( jQuery.isFunction( url ) ) + return this.bind("load", url); + + var off = url.indexOf(" "); + if ( off >= 0 ) { + var selector = url.slice(off, url.length); + url = url.slice(0, off); } - } - - var self = this; - - // Request the remote document - jQuery.ajax( type, url, params,function(res){ - - // Inject the HTML into all the matched elements - self.html(res.responseText).each(function(){ - // If a callback function was provided - if ( callback && callback.constructor == Function ) - // Execute it within the context of the element - callback.apply( self, [res.responseText] ); - }); - - // Execute all the scripts inside of the newly-injected HTML - $("script", self).each(function(){ - eval( this.text || this.textContent || this.innerHTML || ""); + + callback = callback || function(){}; + + // Default to a GET request + var type = "GET"; + + // If the second parameter was provided + if ( params ) + // If it's a function + if ( jQuery.isFunction( params ) ) { + // We assume that it's the callback + callback = params; + params = null; + + // Otherwise, build a param string + } else { + params = jQuery.param( params ); + type = "POST"; + } + + var self = this; + + // Request the remote document + jQuery.ajax({ + url: url, + type: type, + data: params, + complete: function(res, status){ + // If successful, inject the HTML into all the matched elements + if ( status == "success" || status == "notmodified" ) + // See if a selector was specified + self.html( selector ? + // Create a dummy div to hold the results + jQuery("
") + // inject the contents of the document in, removing the scripts + // to avoid any 'Permission Denied' errors in IE + .append(res.responseText.replace(/