-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathget_json.js
35 lines (27 loc) · 908 Bytes
/
get_json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
getJSON(
'http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?',
function(data){
console.log(data);
}
);
*/
(function(){
var id = 0, head = $$('head')[0], global = this;
global.getJSON = function(url, callback) {
var script = document.createElement('script'), token = '__jsonp' + id;
// callback should be a global function
global[token] = callback;
// url should have "?" parameter which is to be replaced with a global callback name
script.src = url.replace(/\?(&|$)/, '__jsonp' + id + '$1');
// clean up on load: remove script tag, null script variable and delete global callback function
script.onload = function() {
script.remove();
script = null;
delete global[token];
};
head.appendChild(script);
// callback name should be unique
id++;
}
})();