Show HN: Cross-browser asynchronous JS script loader w/callbacks
github.com
github.com
Specifically:
1) the window.onload is being directly overwritten without any chaining. The safe way is to first store a reference to whatever was in .onload beforehand and call that in the overwritten onload.
2) the load check uses an interval on a 10ms timer. Chained setTimeouts are much better for performance and 10ms is quite an aggressive default. 50ms gets called 5x fewer and has a imperceptible difference to the user.
3) _async is some big ol' global var with no function scoping at all.
the problem with this solution is you had to include a blocking js script first to use it... a better solution is to inject your script and for callbacks use an array. Look at how GA.js works, this is all it does, push functions onto an Array (builtin) and once the code loads start pop'ing functions to invoke off the array see: https://gist.github.com/3616965
to make it even better replace the push method on the Array with the invoke method meaning once the code's are loaded - you have immediate invoke of functions... someone should spell this out clearer but it makes me sad to not see this technique used more... it's tiny. see: https://gist.github.com/3616991
updated with two version and formated.
yepnope is an asynchronous conditional resource loader that's super-fast, and allows you to load only the scripts that your users need. 1.7kb minified and gzipped. Check it out!
https://github.com/SlexAxton/yepnope.js
Another Script Loader?#?@$!@# - Why Use Yepnope? - quoting conclusion from the page:
> In short, we want you to make the right decision for your application. We can certainly think of times when yepnope.js would not be the correct choice for a resource loader, and we hope we've made those situations abundantly clear in our documentation. However, we also think that yepnope meets a lot of people where they are, when it comes to a balance of performance, usability, size and feature-set.
> If you have questions about if yepnope is right for your application. Send us a tweet or an email and we'll help you out. Most of us in the script-loader author scene are total BFFs and we aren't about competition, we're about pushing the edge. In other words, we totally use LABjs and RequireJS in a bunch of our projects, because they're badass (and the other ones are probably great too!).
Also, as others have mentioned, there are plenty of options out there, so it might be useful highlight any specific difference or advantage this solution provides over LabJS, RequireJS, Yepnope, curl.js etc
On a side note, there is a rather comprehensive feature matrix of a number of popular JS loaders https://spreadsheets.google.com/lv?key=tDdcrv9wNQRCNCRCflWxh...
i (originally) took the repo down b/c so many people starred it and i didnt want them to be scarred :) never saw that spreadsheet b4, thx for posting. a lot of good discussion in this thread. the project was just a little hack rewrite, i never stated anywhere it was better than anything else and you guys can see why :) feedback is great. i did run some tests on it and it is surprisingly almost as fast/faster +/- as similar solutions though! haha.
I'd keep the repo up! (even if the feedback is a bit daunting)
https://github.com/hay/jsdynaload
Allows you to do something like this:
jsDynaLoad(['1.js', '2.js', '3.js'], function() { console.log('callback'); });
This library would allow you to run it asynchronously, so that other js code can continue to run.