X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;ds=inline;f=src%2Fajax%2Fajax.js;h=ccf4f14a95f89048b842a048b5b5984a8433e95b;hb=042a46386a4e5efe787f963245aa534663559838;hp=bd69704de07daeb7b3d59c2b36a902a2e128f3e8;hpb=b0c3711d3e8bd844d1cfa8569864df1c1f73180a;p=jquery.git
diff --git a/src/ajax/ajax.js b/src/ajax/ajax.js
index bd69704..ccf4f14 100644
--- a/src/ajax/ajax.js
+++ b/src/ajax/ajax.js
@@ -1,131 +1,160 @@
-// AJAX Plugin
-// Docs Here:
-// http://jquery.com/docs/ajax/
+jQuery.fn.extend({
-/**
- * Load HTML from a remote file and inject it into the DOM, only if it's
- * been modified by the server.
- *
- * @example $("#feeds").loadIfModified("feeds.html")
- * @before
- * @result
45 feeds found.
- *
- * @name loadIfModified
- * @type jQuery
- * @param String url The URL of the HTML file to load.
- * @param Hash params A set of key/value pairs that will be sent to the server.
- * @param Function callback A function to be executed whenever the data is loaded.
- * @cat AJAX
- */
-jQuery.fn.loadIfModified = function( url, params, callback ) {
- this.load( url, params, callback, 1 );
-};
+ /**
+ * Load HTML from a remote file and inject it into the DOM, only if it's
+ * been modified by the server.
+ *
+ * @example $("#feeds").loadIfModified("feeds.html");
+ * @before
+ * @result
45 feeds found.
+ *
+ * @name loadIfModified
+ * @type jQuery
+ * @param String url The URL of the HTML file to load.
+ * @param Map params (optional) Key/value pairs that will be sent to the server.
+ * @param Function callback (optional) A function to be executed whenever the data is loaded (parameters: responseText, status and response itself).
+ * @cat Ajax
+ */
+ // DEPRECATED
+ loadIfModified: function( url, params, callback ) {
+ this.load( url, params, callback, 1 );
+ },
-/**
- * Load HTML from a remote file and inject it into the DOM.
- *
- * @example $("#feeds").load("feeds.html")
- * @before
- * @result
45 feeds found.
- *
- * @name load
- * @type jQuery
- * @param String url The URL of the HTML file to load.
- * @param Hash params A set of key/value pairs that will be sent to the server.
- * @param Function callback A function to be executed whenever the data is loaded.
- * @cat AJAX
- */
-jQuery.fn.load = function( url, params, callback, ifModified ) {
- if ( url.constructor == Function )
- return this.bind("load", url);
-
- 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 ( 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";
+ /**
+ * Load HTML from a remote file and inject it into the DOM.
+ *
+ * Note: Avoid to use this to load scripts, instead use $.getScript.
+ * IE strips script tags when there aren't any other characters in front of it.
+ *
+ * @example $("#feeds").load("feeds.html");
+ * @before
+ * @result
45 feeds found.
+ *
+ * @example $("#feeds").load("feeds.html",
+ * {limit: 25},
+ * function() { alert("The last 25 entries in the feed have been loaded"); }
+ * );
+ * @desc Same as above, but with an additional parameter
+ * and a callback that is executed when the data was loaded.
+ *
+ * @name load
+ * @type jQuery
+ * @param String url The URL of the HTML file to load.
+ * @param Object params (optional) A set of key/value pairs that will be sent as data to the server.
+ * @param Function callback (optional) A function to be executed whenever the data is loaded (parameters: responseText, status and response itself).
+ * @cat Ajax
+ */
+ load: function( url, params, callback, ifModified ) {
+ 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, status){
-
- if ( status == "success" || !ifModified && status == "notmodified" ) {
- // Inject the HTML into all the matched elements
- self.html(res.responseText).each( callback, [res.responseText, status] );
-
- // Execute all the scripts inside of the newly-injected HTML
- $("script", self).each(function(){
- if ( this.src )
- $.getScript( this.src );
- else
- eval.call( window, this.text || this.textContent || this.innerHTML || "" );
- });
- } else
- callback.apply( self, [res.responseText, status] );
- }, ifModified);
-
- return this;
-};
+ callback = callback || function(){};
-/**
- * A function for serializing a set of input elements into
- * a string of data.
- *
- * @example $("input[@type=text]").serialize();
- * @before
- *
- * @after name=John&location=Boston
- * @desc Serialize a selection of input elements to a string
- *
- * @name serialize
- * @type String
- * @cat AJAX
- */
-jQuery.fn.serialize = function(){
- return $.param( this );
-};
-
-// If IE is used, create a wrapper for the XMLHttpRequest object
-if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
- XMLHttpRequest = function(){
- return new ActiveXObject(
- navigator.userAgent.indexOf("MSIE 5") >= 0 ?
- "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
- );
- };
+ // 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,
+ ifModified: ifModified,
+ complete: function(res, status){
+ // If successful, inject the HTML into all the matched elements
+ if ( status == "success" || !ifModified && 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(/