X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fajax%2Fajax.js;h=fa48afab57b1134ad1719ab9312f889f3b11e604;hb=6ac46e6a5a6cac509626267865980f4dfbeebc2d;hp=e5a3854cf0efaff7fd1484640e129d19b019917e;hpb=7cc550727c4e0bcd93c5d435f0799f568fc74dfa;p=jquery.git
diff --git a/src/ajax/ajax.js b/src/ajax/ajax.js
index e5a3854..fa48afa 100644
--- a/src/ajax/ajax.js
+++ b/src/ajax/ajax.js
@@ -122,9 +122,10 @@ jQuery.fn.extend({
return this.find('script').each(function(){
if ( this.src )
// for some weird reason, it doesn't work if the callback is ommited
- jQuery.getScript( this.src, function() {} );
- else
- eval.call( window, this.text || this.textContent || this.innerHTML || "" );
+ jQuery.getScript( this.src );
+ else {
+ jQuery.globalEval( this.text || this.textContent || this.innerHTML || "" );
+ }
}).end();
}
@@ -133,10 +134,7 @@ jQuery.fn.extend({
// 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"
- );
+ return new ActiveXObject("Microsoft.XMLHTTP");
};
// Attach a bunch of functions for handling common AJAX events
@@ -172,7 +170,10 @@ if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
/**
* Attach a function to be executed whenever an AJAX request completes.
*
- * @example $("#msg").ajaxComplete(function(){
+ * The XMLHttpRequest and settings used for that request are passed
+ * as arguments to the callback.
+ *
+ * @example $("#msg").ajaxComplete(function(request, settings){
* $(this).append("
Request Complete.");
* });
* @desc Show a message when an AJAX request completes.
@@ -187,7 +188,10 @@ if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
* Attach a function to be executed whenever an AJAX request completes
* successfully.
*
- * @example $("#msg").ajaxSuccess(function(){
+ * The XMLHttpRequest and settings used for that request are passed
+ * as arguments to the callback.
+ *
+ * @example $("#msg").ajaxSuccess(function(request, settings){
* $(this).append("Successful Request!");
* });
* @desc Show a message when an AJAX request completes successfully.
@@ -201,8 +205,11 @@ if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
/**
* Attach a function to be executed whenever an AJAX request fails.
*
- * @example $("#msg").ajaxError(function(){
- * $(this).append("Error requesting page.");
+ * The XMLHttpRequest and settings used for that request are passed
+ * as arguments to the callback.
+ *
+ * @example $("#msg").ajaxError(function(request, settings){
+ * $(this).append("Error requesting page " + settings.url + "");
* });
* @desc Show a message when an AJAX request fails.
*
@@ -211,9 +218,26 @@ if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
* @param Function callback The function to execute.
* @cat AJAX
*/
+
+/**
+ * Attach a function to be executed before an AJAX request is send.
+ *
+ * The XMLHttpRequest and settings used for that request are passed
+ * as arguments to the callback.
+ *
+ * @example $("#msg").ajaxSend(function(request, settings){
+ * $(this).append("Starting request at " + settings.url + "");
+ * });
+ * @desc Show a message before an AJAX request is send.
+ *
+ * @name ajaxSend
+ * @type jQuery
+ * @param Function callback The function to execute.
+ * @cat AJAX
+ */
new function(){
- var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess".split(",");
+ var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(",");
for ( var i = 0; i < e.length; i++ ) new function(){
var o = e[i];
@@ -303,6 +327,10 @@ jQuery.extend({
* Loads, and executes, a remote JavaScript file using an HTTP GET request.
* All of the arguments to the method (except URL) are optional.
*
+ * Warning: Safari <= 2.0.x is unable to evalulate scripts in a global
+ * context sychronously. If you load functions via getScript, make sure
+ * to call them after a delay.
+ *
* @example $.getScript("test.js")
*
* @example $.getScript("test.js", function(){
@@ -467,7 +495,7 @@ jQuery.extend({
* function gets passed two arguments: The XMLHttpRequest object and a
* string describing the type the success of the request.
*
- * (String) data - Data to be sent to the server. Converted to a query
+ * (Object|String) data - Data to be sent to the server. Converted to a query
* string, if not already a string. Is appended to the url for GET-requests.
* Override processData option to prevent processing.
*
@@ -482,6 +510,9 @@ jQuery.extend({
* (Boolean) async - By default, all requests are send asynchronous (set to true).
* If you need synchronous requests, set this option to false.
*
+ * (Function) beforeSend - A pre-callback to set custom headers etc., the
+ * XMLHttpRequest is passed as the only argument.
+ *
* @example $.ajax({
* type: "GET",
* url: "test.js",
@@ -519,7 +550,8 @@ jQuery.extend({
data: null,
contentType: "application/x-www-form-urlencoded",
processData: true,
- async: true
+ async: true,
+ beforeSend: null
}, s);
// if data available
@@ -560,6 +592,12 @@ jQuery.extend({
// Make sure the browser sends the right content length
if ( xml.overrideMimeType )
xml.setRequestHeader("Connection", "close");
+
+ // Allow custom headers/mimetypes
+ if( s.beforeSend )
+ s.beforeSend(xml);
+ if (s.global)
+ jQuery.event.trigger("ajaxSend", [xml, s]);
// Wait for a response to come back
var onreadystatechange = function(isTimeout){
@@ -581,13 +619,16 @@ jQuery.extend({
if ( s.ifModified && modRes )
jQuery.lastModified[s.url] = modRes;
- // If a local callback was specified, fire it
+ // process the data (runs the xml through httpData regardless of callback)
+ var data = jQuery.httpData( xml, s.dataType );
+
+ // If a local callback was specified, fire it and pass it the data
if ( s.success )
- s.success( jQuery.httpData( xml, s.dataType ), status );
+ s.success( data, status );
// Fire the global callback
if( s.global )
- jQuery.event.trigger( "ajaxSuccess" );
+ jQuery.event.trigger( "ajaxSuccess", [xml, s] );
// Otherwise, the request was not successful
} else {
@@ -596,12 +637,12 @@ jQuery.extend({
// Fire the global callback
if( s.global )
- jQuery.event.trigger( "ajaxError" );
+ jQuery.event.trigger( "ajaxError", [xml, s] );
}
// The request was completed
if( s.global )
- jQuery.event.trigger( "ajaxComplete" );
+ jQuery.event.trigger( "ajaxComplete", [xml, s] );
// Handle the global AJAX counter
if ( s.global && ! --jQuery.active )
@@ -678,8 +719,10 @@ jQuery.extend({
var data = !type && ct && ct.indexOf("xml") >= 0;
data = type == "xml" || data ? r.responseXML : r.responseText;
- // If the type is "script", eval it
- if ( type == "script" ) eval.call( window, data );
+ // If the type is "script", eval it in global context
+ if ( type == "script" ) {
+ jQuery.globalEval( data );
+ }
// Get the JavaScript object, if JSON is used.
if ( type == "json" ) eval( "data = " + data );
@@ -706,10 +749,10 @@ jQuery.extend({
} else {
// Serialize the key/values
for ( var j in a ) {
- //if one value is array then treat each array value in part
- if (typeof a[j] == 'object') {
+ // If the value is an array then the key names need to be repeated
+ if( a[j].constructor == Array ) {
for (var k = 0; k < a[j].length; k++) {
- s.push( j + "[]=" + encodeURIComponent( a[j][k] ) );
+ s.push( j + "=" + encodeURIComponent( a[j][k] ) );
}
} else {
s.push( j + "=" + encodeURIComponent( a[j] ) );
@@ -719,6 +762,18 @@ jQuery.extend({
// Return the resulting serialization
return s.join("&");
+ },
+
+ // evalulates a script in global context
+ // not reliable for safari
+ globalEval: function(data) {
+ if (window.execScript)
+ window.execScript( data );
+ else if(jQuery.browser.safari)
+ // safari doesn't provide a synchronous global eval
+ window.setTimeout( data, 0 );
+ else
+ eval.call( window, data );
}
});