Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add getJSON for working with JSONP

  • Loading branch information...
commit c4134bde1a61a1c6fcffe0f7f59cfa864897499a 1 parent 6963dd4
kangax authored
Showing with 41 additions and 3 deletions.
  1. +3 −3 README.rdoc
  2. +35 −0 get_json.js
  3. +3 −0  lib/prototype_rhino_env.js
View
6 README.rdoc
@@ -17,9 +17,9 @@ Protolicious is a set of javascript snippets based on prototype.js
* <tt>Function#negate</tt>
* <tt>Function#runOnce</tt>
* <tt>Function#_new</tt>
-* <tt>Function#toDelayed()</tt>
-* <tt>Function#toDeferred()</tt>
-* <tt>Function#addAdvice()</tt>
+* <tt>Function#toDelayed</tt>
+* <tt>Function#toDeferred</tt>
+* <tt>Function#addAdvice</tt>
== Array extensions
View
35 get_json.js
@@ -0,0 +1,35 @@
+/*
+
+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++;
+ }
+})();
+
View
3  lib/prototype_rhino_env.js
@@ -0,0 +1,3 @@
+if (!window) window = { };
+if (!navigator) navigator = { userAgent: 'Gecko' };
+if (!window.document) window.document = { };
Please sign in to comment.
Something went wrong with that request. Please try again.