Skip to content

Instantly share code, notes, and snippets.

@tobyhede
Forked from juliocesar/vsjsonp.js
Last active August 29, 2015 14:13
Show Gist options
  • Save tobyhede/aa37fb34e40346cf02c4 to your computer and use it in GitHub Desktop.
Save tobyhede/aa37fb34e40346cf02c4 to your computer and use it in GitHub Desktop.

Revisions

  1. @juliocesar juliocesar created this gist Jan 12, 2015.
    24 changes: 24 additions & 0 deletions vsjsonp.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // VSJONP ― Very Simple JSONP
    // ==========================
    //
    // Usage:
    // fetchJsonP({
    // url: 'http://shit-no-cors.json',
    // complete: function(response) {
    // console.log(response);
    // }
    // });

    var fetchJsonP = function(options) {
    var options = options ? options : {},
    script = document.createElement('script');
    functionName = 'jsonp' + Math.floor(Math.random() * 9999);
    window[functionName] = function(response) {
    if (options.complete) options.complete(response);
    }
    script.src = options.url + "&callback=" + functionName;
    script.onload = function() {
    document.body.removeChild(script);
    delete window[functionName];
    }
    }