/**jQueryJSONPCorePlugin2.3.1(2012-05-16)**https://github.com/jaubourg/jquery-jsonp**Copyright(c)2012JulianAubourg**Thisdocumentislicensedasfreesoftwareunderthetermsofthe*MITLicense:http://www.opensource.org/licenses/mit-license.php*/( function( $ ) { //######################UTILITIES##// Noopfunctionnoop(){}// Generic callbackfunctiongenericCallback(data){lastValue=[data];}// Call if definedfunctioncallIfDefined(method,object,parameters,returnFlag){try{returnFlag=method&&method.apply(object.context||object,parameters);}catch(_){returnFlag=!1;}returnreturnFlag;}// Give joining character given urlfunctionqMarkOrAmp(url){return/\?/.test(url)?"&":"?";}var// String constants (for better minification)STR_ASYNC="async",STR_CHARSET="charset",STR_EMPTY="",STR_ERROR="error",STR_INSERT_BEFORE="insertBefore",STR_JQUERY_JSONP="_jqjsp",STR_ON="on",STR_ON_CLICK=STR_ON+"click",STR_ON_ERROR=STR_ON+STR_ERROR,STR_ON_LOAD=STR_ON+"load",STR_ON_READY_STATE_CHANGE=STR_ON+"readystatechange",STR_READY_STATE="readyState",STR_REMOVE_CHILD="removeChild",STR_SCRIPT_TAG="<script>",STR_SUCCESS="success",STR_TIMEOUT="timeout",// Windowwin=window,// DeferredDeferred=$.Deferred,// Head elementhead=$("head")[0]||document.documentElement,// Page cachepageCache={},// Countercount=0,// Last returned valuelastValue,// ###################### DEFAULT OPTIONS ##xOptionsDefaults={//beforeSend: undefined,//cache: false,callback:STR_JQUERY_JSONP,//callbackParameter: undefined,//charset: undefined,//complete: undefined,//context: undefined,//data: "",//dataFilter: undefined,//error: undefined,//pageCache: false,//success: undefined,//timeout: 0,//traditional: false,url:location.href},// opera demands sniffing :/opera=win.opera;// ###################### MAIN FUNCTION ##functionjsonp(xOptions){// Build data with defaultxOptions=$.extend({},xOptionsDefaults,xOptions);// References to xOptions members (for better minification)varsuccessCallback=xOptions.success,errorCallback=xOptions.error,completeCallback=xOptions.complete,dataFilter=xOptions.dataFilter,callbackParameter=xOptions.callbackParameter,successCallbackName=xOptions.callback,cacheFlag=xOptions.cache,pageCacheFlag=xOptions.pageCache,charset=xOptions.charset,url=xOptions.url,data=xOptions.data,timeout=xOptions.timeout,pageCached,// Abort/done flagdone=0,// Life-cycle functionscleanUp=noop,// Support varssupportOnload,supportOnreadystatechange,// Request execution varsfirstChild,script,scriptAfter,timeoutTimer;// If we have Deferreds:// - substitute callbacks// - promote xOptions to a promiseDeferred&&Deferred(function(defer){defer.done(successCallback).fail(errorCallback);successCallback=defer.resolve;errorCallback=defer.reject;}).promise(xOptions);// Create the abort methodxOptions.abort=function(){!(done++)&&cleanUp();};// Call beforeSend if provided (early abort if false returned)if(callIfDefined(xOptions.beforeSend,xOptions,[xOptions])===!1||done){returnxOptions;}// Control entriesurl=url||STR_EMPTY;data=data?((typeofdata)=="string"?data:$.param(data,xOptions.traditional)):STR_EMPTY;// Build final urlurl+=data?(qMarkOrAmp(url)+data):STR_EMPTY;// Add callback parameter if provided as optioncallbackParameter&&(url+=qMarkOrAmp(url)+encodeURIComponent(callbackParameter)+"=?");// Add anticache parameter if needed!cacheFlag&&!pageCacheFlag&&(url+=qMarkOrAmp(url)+"_"+(newDate()).getTime()+"=");// Replace last ? by callback parameterurl=url.replace(/=\?(&|$)/,"="+successCallbackName+"$1");// Success notifierfunctionnotifySuccess(json){if(!(done++)){cleanUp();// Pagecache if neededpageCacheFlag&&(pageCache[url]={s:[json]});// Apply the data filter if provideddataFilter&&(json=dataFilter.apply(xOptions,[json]));// Call success then completecallIfDefined(successCallback,xOptions,[json,STR_SUCCESS]);callIfDefined(completeCallback,xOptions,[xOptions,STR_SUCCESS]);}}// Error notifierfunctionnotifyError(type){if(!(done++)){// Clean upcleanUp();// If pure error (not timeout), cache if neededpageCacheFlag&&type!=STR_TIMEOUT&&(pageCache[url]=type);// Call error then completecallIfDefined(errorCallback,xOptions,[xOptions,type]);callIfDefined(completeCallback,xOptions,[xOptions,type]);}}// Check page cacheif(pageCacheFlag&&(pageCached=pageCache[url])){pageCached.s?notifySuccess(pageCached.s[0]):notifyError(pageCached);}else{// Install the generic callback// (BEWARE: global namespace pollution ahoy)win[successCallbackName]=genericCallback;// Create the script tagscript=$(STR_SCRIPT_TAG)[0];script.id=STR_JQUERY_JSONP+count++;// Set charset if providedif(charset){script[STR_CHARSET]=charset;}opera&&opera.version()<11.60?// onerror is not supported: do not set as async and assume in-order execution.// Add a trailing script to emulate the event((scriptAfter=$(STR_SCRIPT_TAG)[0]).text="document.getElementById('"+script.id+"')."+STR_ON_ERROR+"()"):// onerror is supported: set the script as async to avoid requests blocking each others(script[STR_ASYNC]=STR_ASYNC);// Internet Explorer: event/htmlFor trickif(STR_ON_READY_STATE_CHANGEinscript){script.htmlFor=script.id;script.event=STR_ON_CLICK;}// Attached event handlersscript[STR_ON_LOAD]=script[STR_ON_ERROR]=script[STR_ON_READY_STATE_CHANGE]=function(result){// Test readyState if it existsif(!script[STR_READY_STATE]||!/i/.test(script[STR_READY_STATE])){try{script[STR_ON_CLICK]&&script[STR_ON_CLICK]();}catch(_){}result=lastValue;lastValue=0;result?notifySuccess(result[0]):notifyError(STR_ERROR);}};// Set sourcescript.src=url;// Re-declare cleanUp functioncleanUp=function(i){timeoutTimer&&clearTimeout(timeoutTimer);script[STR_ON_READY_STATE_CHANGE]=script[STR_ON_LOAD]=script[STR_ON_ERROR]=null;head[STR_REMOVE_CHILD](script);scriptAfter&&head[STR_REMOVE_CHILD](scriptAfter);};// Append main scripthead[STR_INSERT_BEFORE](script,(firstChild=head.firstChild));// Append trailing script if neededscriptAfter&&head[STR_INSERT_BEFORE](scriptAfter,firstChild);// If a timeout is needed, install ittimeoutTimer=timeout>0&&setTimeout(function(){notifyError(STR_TIMEOUT);},timeout);}returnxOptions;}// ###################### SETUP FUNCTION ##jsonp.setup=function(xOptions){$.extend(xOptionsDefaults,xOptions);};// ###################### INSTALL in jQuery ##$.jsonp=jsonp;})(jQuery);