Skip to content

Instantly share code, notes, and snippets.

@furf
Created February 17, 2010 20:49
Show Gist options
  • Save furf/306998 to your computer and use it in GitHub Desktop.
Save furf/306998 to your computer and use it in GitHub Desktop.
jQuery.useJsonpCallback = (jQuery.support.jsonpCallback = parseFloat(jQuery.fn.jquery) >= 1.4) ? jQuery.noop : function () {
var cfg = this, jsonp = cfg.dataType === 'script' && cfg.jsonpCallback;
if (jsonp && !window[jsonp]) {
window[jsonp] = function (data) {
cfg.success.call(cfg, data, 'success');
try {
delete window[jsonp];
} catch (ie6) {
// cannot delete window properties in IE6
window[jsonp] = undefined;
}
};
}
};
jQuery.useJsonpCallback=(jQuery.support.jsonpCallback=parseFloat(jQuery.fn.jquery)>=1.4)?jQuery.noop:function(){var a=this,b=a.dataType==="script"&&a.jsonpCallback;if(b&&!window[b]){window[b]=function(d){a.success.call(a,d,"success");try{delete window[b];}catch(c){window[b]=undefined;}};}};
/**
* The jQuery.jsonpCallback plugin enables support for the jsonpCallback Ajax setting,
* introduced in jQuery 1.4, to earlier versions of jQuery. This property is useful in
* situations where dynamic naming of a JSON-P callback is not possible, ie. environments
* with cached services.
*
* This plugin is backward- and forward-compatible.
*/
/**
* Detecting support
*/
if (jQuery.support.jsonpCallback) {
// jsonpCallback is natively supported
} else {
// jsonpCallback is NOT natively supported
}
/**
* Basic usage
*/
$.ajax({
url: 'test.js',
dataType: 'jsonp',
// Enable jsonpCallback support
beforeSend: $.useJsonpCallback,
// Name fixed jsonpCallback method
jsonpCallback: 'jsonp_callback_method_name',
success: handleSuccess
});
/**
* Usage with a custom beforeSend method
*/
$.ajax({
url: 'test.js',
dataType: 'jsonp',
beforeSend: function () {
// Enable jsonpCallback support
$.useJsonpCallback.apply(this, arguments);
// do something...
},
// Name fixed jsonpCallback method
jsonpCallback: 'jsonp_callback_method_name',
success: handleSuccess
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment