diff --git a/MIT-LICENSE.txt b/MIT-LICENSE.txt new file mode 100644 index 0000000..b4943b0 --- /dev/null +++ b/MIT-LICENSE.txt @@ -0,0 +1,20 @@ +Copyright 2012 Julian Aubourg + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 3b769fb..4ce22fc 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,16 @@ # jQuery-JSONP +[![CDNJS](https://img.shields.io/cdnjs/v/jQuery-JSONP.svg)](https://cdnjs.com/libraries/jQuery-JSONP) + jQuery-JSONP is a compact (1.8kB minified), yet feature-packed, alternative solution to jQuery's implementation of JSONP. +## Licensing + +jQuery-JSONP is released under the [MIT license](https://github.com/jaubourg/jquery-jsonp/blob/master/MIT-LICENSE.txt). + ## Download -You can download jQuery-JSONP [here](/jaubourg/jquery-jsonp/downloads) (full text and minified versions available). +You can download jQuery-JSONP [here](https://github.com/jaubourg/jquery-jsonp/downloads) (full text and minified versions available). ## Features @@ -33,5 +39,5 @@ jQuery-JSONP has also been tested with jQuery 1.3.x up to 1.7.x ## Documentation -* [API Documentation](/jaubourg/jquery-jsonp/blob/master/doc/API.md) -* [Tips & Tricks](/jaubourg/jquery-jsonp/blob/master/doc/TipsAndTricks.md) +* [API Documentation](https://github.com/jaubourg/jquery-jsonp/blob/master/doc/API.md) +* [Tips & Tricks](https://github.com/jaubourg/jquery-jsonp/blob/master/doc/TipsAndTricks.md) diff --git a/doc/API.md b/doc/API.md index 80bc3f0..2c25eee 100644 --- a/doc/API.md +++ b/doc/API.md @@ -125,10 +125,10 @@ Lifetime of this cache is page-based which means it gets erased at each page rel #### success - `function` (`undefined`) -A function to be called if the request succeeds. The function gets passed two arguments: The JSON object returned from the server and a string describing the status (always `"success"`). +A function to be called if the request succeeds. The function gets passed three arguments: The JSON object returned from the server, a string describing the status (always `"success"`) and, *_as of version 2.4.0_* the `xOptions` object. ```js -function (json, textStatus) { +function (json, textStatus, xOptions) { this; // the xOptions object or xOptions.context if provided } ``` @@ -155,4 +155,4 @@ Setup global settings for JSONP requests. ### Options -See $.jsonp for a description of all available options. \ No newline at end of file +See $.jsonp for a description of all available options. diff --git a/package.json b/package.json new file mode 100644 index 0000000..d59c727 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "jquery-jsonp", + "version": "2.4.0", + "description": "Alternative solution to jQuery's implementation of JSONP", + "main": "src/jquery.jsonp.js", + "directories": { + "doc": "doc", + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/jaubourg/jquery-jsonp.git" + }, + "keywords": [ + "JSONP", + "jQuery" + ], + "author": "Julian Aubourg", + "license": "MIT", + "bugs": { + "url": "https://github.com/jaubourg/jquery-jsonp/issues" + }, + "homepage": "https://github.com/jaubourg/jquery-jsonp" +} diff --git a/src/jquery.jsonp.js b/src/jquery.jsonp.js index 5414f64..2abeef7 100644 --- a/src/jquery.jsonp.js +++ b/src/jquery.jsonp.js @@ -1,5 +1,5 @@ /* - * jQuery JSONP Core Plugin 2.3.1 (2012-05-16) + * jQuery JSONP Core Plugin 2.4.0 (2012-08-21) * * https://github.com/jaubourg/jquery-jsonp * @@ -22,13 +22,8 @@ } // Call if defined - function callIfDefined( method , object , parameters , returnFlag ) { - try { - returnFlag = method && method.apply( object.context || object , parameters ); - } catch( _ ) { - returnFlag = !1; - } - return returnFlag; + function callIfDefined( method , object , parameters ) { + return method && method.apply( object.context || object , parameters ); } // Give joining character given url @@ -87,7 +82,10 @@ }, // opera demands sniffing :/ - opera = win.opera; + opera = win.opera, + + // IE < 10 + oldIE = !!$( "
" ).html( "" ).find("i").length; // ###################### MAIN FUNCTION ## function jsonp( xOptions ) { @@ -141,7 +139,7 @@ }; // Call beforeSend if provided (early abort if false returned) - if ( callIfDefined( xOptions.beforeSend, xOptions , [ xOptions ] ) === !1 || done ) { + if ( callIfDefined( xOptions.beforeSend , xOptions , [ xOptions ] ) === !1 || done ) { return xOptions; } @@ -172,7 +170,7 @@ // Apply the data filter if provided dataFilter && ( json = dataFilter.apply( xOptions , [ json ] ) ); // Call success then complete - callIfDefined( successCallback , xOptions , [ json , STR_SUCCESS ] ); + callIfDefined( successCallback , xOptions , [ json , STR_SUCCESS, xOptions ] ); callIfDefined( completeCallback , xOptions , [ xOptions , STR_SUCCESS ] ); } @@ -225,8 +223,7 @@ ; // Internet Explorer: event/htmlFor trick - if ( STR_ON_READY_STATE_CHANGE in script ) { - + if ( oldIE ) { script.htmlFor = script.id; script.event = STR_ON_CLICK; } diff --git a/test/unit.js b/test/unit.js index c0b6c30..273bc25 100644 --- a/test/unit.js +++ b/test/unit.js @@ -1,6 +1,6 @@ module( "main", { teardown: moduleTeardown } ); -var hasDeferred = $.Deferred ? 1 : 0; +var hasDeferred = !!$.Deferred; function testJSONP( name, outcome, options ) { test( name, function() { @@ -44,7 +44,7 @@ testJSONP( "error (HTTP Code)", "error", { } }); -if ( window.opera && window.opera.version() < 11.60 ) { +if ( !window.opera || window.opera.version() < 11.60 ) { testJSONP( "error (Syntax Error)", "error", { expect: window.opera ? 0 : 1, url: "data/syntax-error.js", @@ -61,6 +61,31 @@ if ( window.opera && window.opera.version() < 11.60 ) { }); } +if ( !window.opera ) { + test( "error (Exception)", function() { + stop(); + expect( 2 ); + $.jsonp({ + url: "http://gdata.youtube.com/feeds/api/users/julianaubourg?callback=?", + beforeSend: function() { + var oldOnError = window.onerror; + window.onerror = function( flag ) { + ok( flag !== undefined, "Exception Thrown" ); + window.onerror = oldOnError; + start(); + }; + }, + success: function() { + ok( true, "success" ); + throw "an exception"; + }, + complete: function() { + window.onerror(); + } + }); + }); +} + testJSONP( "error (callback not called)", "error", { expect: 1, url: "data/no-callback.js",