Skip to content

Instantly share code, notes, and snippets.

@zhannes
Created July 22, 2013 00:26
Show Gist options
  • Save zhannes/6050548 to your computer and use it in GitHub Desktop.
Save zhannes/6050548 to your computer and use it in GitHub Desktop.
small jsonp
(function(doc, script){
var API_KEY = ''; // use yours
// tks -- http://css-tricks.com/thinking-async/
var js,
fjs = doc.getElementsByTagName(script)[0],
add = function(url, id) {
if (doc.getElementById(id)) {return;}
js = doc.createElement(script);
js.src = url;
id && (js.id = id);
fjs.parentNode.insertBefore(js, fjs);
};
/* CODE FOR JSONP request, using Tumblr as example*/
var url,
ts = (new Date).getTime(),
cb = 'jsonp_'+ts,
// vars used later as params, adjust for your needs
apiKey = API_KEY,
apiBase = 'http://api.tumblr.com/v2/blog/',
baseHostName = window.location.host;
window[cb] = handleResponse;
function handleResponse(data){
console.log(data);
}
// adapt for your needs
url = [
apiBase,
baseHostName,
'/posts?api_key=',
apiKey,
'&type=photo',
'&tag=responsive',
'&callback='+callbackName,
'&cacheBust='+ (new Date).getTime()
].join('');
add(url);
/* END JSONP */
})(document, 'script');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment