Skip to content

Instantly share code, notes, and snippets.

@tvpmb
Created June 18, 2012 17:02
Show Gist options
  • Select an option

  • Save tvpmb/2949412 to your computer and use it in GitHub Desktop.

Select an option

Save tvpmb/2949412 to your computer and use it in GitHub Desktop.
Load External Libraries, via a simple AJAX call
(function() {
device.ajax(
{
url: 'http://underscorejs.org/underscore-min.js',
type: 'GET',
headers: {
'Content-Type': 'text/javascript'
}
},
function onSuccess(body, textStatus, response) {
eval(body);
if (_) console.log('underscore.js loaded successfully');
yourCode(_);
},
function onError(textStatus, response){
console.error('error code: ', response.status);
});
})();
function yourCode(_) {
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
console.info(_.pluck(stooges, 'name'));
}
Credit to Chris Missal & his blog post for this one: http://lostechies.com/chrismissal/2012/06/08/load-external-javascript-files-in-onx/
- This could be used to load libraries from Facebook or ShareThis. External libraries like this are obscenely large and take quite a bit of time to load. In the case of ShareThis, I've found that 1-2 out of 20 times, their system/service has a major timeout issue when it hits the oAuth process.
- Additionally, ShareThis halts the load of your page - doesn't work very well for AMD based JS websites.
- I don't like the eval that is present, not sure if there is anything that could be done about that though.
- Another potential way to achieve this would be to just add a new <script /> tag to the <head /> of the page when you need to load the library....however, you do lose the possibility of having jQuery Deferred execution.
@yairEO
Copy link
Copy Markdown

yairEO commented Oct 4, 2018

what is device.ajax ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment