Skip to content

Instantly share code, notes, and snippets.

@gf3
Created June 18, 2009 18:18

Revisions

  1. gf3 revised this gist Aug 5, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions jsonp.js
    Original file line number Diff line number Diff line change
    @@ -17,13 +17,13 @@
    * )
    **/

    var loadJSONP = (function loadJSONP( window, document, undefined ) { var uuid, head, main_script
    var loadJSONP = (function loadJSONP_outer( window, document, undefined ) { var uuid, head, main_script
    uuid = 0
    head = document.head || document.getElementsByTagName( 'head' )[0]
    main_script = document.createElement( 'script' )
    main_script.type = 'text/javascript'

    return function( url, callback, context ) { var name, script
    return function loadJSONP_inner( url, callback, context ) { var name, script
    // INIT
    name = '__jsonp_' + uuid++
    if ( url.match(/\?/) )
  2. gf3 revised this gist Aug 5, 2010. 2 changed files with 50 additions and 36 deletions.
    36 changes: 0 additions & 36 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,36 +0,0 @@
    /*
    loadJSONP(
    'http://www.gigpark.com/businesses/runlevel6.json',
    function(data) {
    console.log(data);
    }
    );
    */

    var loadJSONP = (function(){
    var unique = 0;
    return function(url, callback, context) {
    // INIT
    var name = "__jsonp_" + unique++;
    if (url.match(/\?/)) url += "&callback="+name;
    else url += "?callback="+name;

    // Create script
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;

    // Setup handler
    window[name] = function(data){
    callback.call((context || window), data);
    document.getElementsByTagName('head')[0].removeChild(script);
    script = null;
    delete window[name];
    };

    // Load JSON
    document.getElementsByTagName('head')[0].appendChild(script);
    };
    })();
    50 changes: 50 additions & 0 deletions jsonp.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    /**
    * loadJSONP( url, hollaback [, context] ) -> Null
    * - url (String): URL to data resource.
    * - hollaback (Function): Function to call when data is successfully loaded,
    * it receives one argument: the data.
    * - context (Object): Context to invoke the hollaback function in.
    *
    * Load external data through a JSONP interface.
    *
    * ### Examples
    *
    * loadJSONP(
    * 'http://www.gigpark.com/businesses/runlevel6.json',
    * function(data) {
    * console.log(data)
    * }
    * )
    **/

    var loadJSONP = (function loadJSONP( window, document, undefined ) { var uuid, head, main_script
    uuid = 0
    head = document.head || document.getElementsByTagName( 'head' )[0]
    main_script = document.createElement( 'script' )
    main_script.type = 'text/javascript'

    return function( url, callback, context ) { var name, script
    // INIT
    name = '__jsonp_' + uuid++
    if ( url.match(/\?/) )
    url += '&callback=' + name
    else
    url += '?callback=' + name

    // Create script
    script = main_script.cloneNode()
    script.src = url

    // Setup handler
    window[name] = function( data ) {
    callback.call( ( context || window ), data )
    head.removeChild( script )
    script = null
    delete window[name]
    }

    // Load JSON
    head.appendChild( script )
    }
    })( window, document )

  3. gf3 revised this gist Jun 18, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ var loadJSONP = (function(){
    var unique = 0;
    return function(url, callback, context) {
    // INIT
    var name = "_jsonp_" + unique++;
    var name = "__jsonp_" + unique++;
    if (url.match(/\?/)) url += "&callback="+name;
    else url += "?callback="+name;

  4. gf3 revised this gist Jun 18, 2009. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,14 @@
    /*
    loadJSONP(
    'http://www.gigpark.com/businesses/runlevel6.json',
    function(data) {
    console.log(data);
    }
    );
    */

    var loadJSONP = (function(){
    var unique = 0;
    return function(url, callback, context) {
  5. gf3 revised this gist Jun 18, 2009. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,7 @@ var loadJSONP = (function(){
    window[name] = function(data){
    callback.call((context || window), data);
    document.getElementsByTagName('head')[0].removeChild(script);
    script = null;
    delete window[name];
    };

  6. gf3 created this gist Jun 18, 2009.
    24 changes: 24 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    var loadJSONP = (function(){
    var unique = 0;
    return function(url, callback, context) {
    // INIT
    var name = "_jsonp_" + unique++;
    if (url.match(/\?/)) url += "&callback="+name;
    else url += "?callback="+name;

    // Create script
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;

    // Setup handler
    window[name] = function(data){
    callback.call((context || window), data);
    document.getElementsByTagName('head')[0].removeChild(script);
    delete window[name];
    };

    // Load JSON
    document.getElementsByTagName('head')[0].appendChild(script);
    };
    })();