Created
December 16, 2012 06:49
-
-
Save anonymous/4303892 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adds one JSONP global (for requireJS module, see JSONP.js) | |
var JSONP = (function (global) { | |
// (C) WebReflection Essential - Mit Style | |
// cleaned up by Brett Zamir for JSLint and avoiding additional globals and need for conventional [?&]callback= in URL) | |
'use strict'; | |
var id = 0, | |
ns = 'JSONP', | |
prefix = '__JSONP__', | |
document = global.document, | |
documentElement = document.documentElement; | |
return function (uri, callback) { | |
var src = prefix + id++, | |
script = document.createElement('script'), | |
JSONPResponse = function () { | |
try { delete global[ns][src]; } catch(e) { global[ns][src] = null; } | |
documentElement.removeChild(script); | |
callback.apply(this, arguments); | |
}; | |
global[ns][src] = JSONPResponse; | |
documentElement.insertBefore( | |
script, | |
documentElement.lastChild | |
).src = uri + (uri.indexOf('?') > -1 ? '&' : '?') + 'callback=' + ns + '.' + src; | |
}; | |
}(this)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*globals define */ | |
define(function () { | |
// (C) WebReflection Essential - Mit Style | |
// cleaned up by Brett Zamir for JSLint and avoiding additional globals and need for conventional [?&]callback= in URL) | |
'use strict'; | |
var id = 0, | |
global = this, | |
ns = 'JSONP', | |
prefix = '__JSONP__', | |
document = global.document, | |
documentElement = document.documentElement; | |
return function (uri, callback) { | |
var src = prefix + id++, | |
script = document.createElement('script'), | |
JSONPResponse = function () { | |
try { delete global[ns][src]; } catch(e) { global[ns][src] = null; } | |
documentElement.removeChild(script); | |
callback.apply(this, arguments); | |
}; | |
global[ns][src] = JSONPResponse; | |
documentElement.insertBefore( | |
script, | |
documentElement.lastChild | |
).src = uri + (uri.indexOf('?') > -1 ? '&' : '?') + 'callback=' + ns + '.' + src; | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*globals define */ | |
define(['JSONP'], function MediaWikiJSONP(baseURL, argObj, cb) { | |
var uri, arg, args = ''; | |
if (!cb) { | |
cb = argObj; | |
argObj = null; | |
} | |
for (arg in argObj) { | |
args += '&' + arg + '=' + encodeURIComponent(argObj[arg]); | |
} | |
uri = baseURL + '/w/api.php?format=json' + args; | |
JSONP(uri, cb); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment