jsonp

// simple jsonp script that fetch result from http://pipes.yahoo.com

function getData(url, callback) {
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    var rnd = Math.random().toString();
    var callbackName = 'jsonp' + rnd.substr(rnd.indexOf('.')+1);
    window[callbackName] = function(tmp) {
        callback(tmp);
        window[callbackName] = undefined;
        try { delete window[callbackName]; } catch(e){}
    }
    script.src = url + '&_callback=' + callbackName;
    script.onload = function(){ head.removeChild(script); };
    head.appendChild(script);
}

// example

// example: fetch flickr streams from pipes
getData('http://pipes.yahoo.com/pipes/pipe.run?_id=7114ce50d2699c7fe2d920a8d2aa17f0&_render=json',
  function(data) { alert(data) });

0 Comments

Post A Comment