14 ms·
Load.js: A small script to lazily load js
- tlrobinson 16y agoIf I understand this correctly you have to figure out the correct order for the dependencies to be loaded yourself. This does not scale. A better solution for larger applications is for individual files to declare their dependencies, then determine the order to load them implicitly. CommonJS modules are the de-facto standard for doing this on the server, and they're starting to be used in the browser as well.
- Swizec 16y agoThat sounds like a pretty silly drawback. A much cleaner solution would be using window.onload to load all javascript and then only starting execution on document.ready. That way you pretty successfully get around the issue of ordered dependancies because at execution time everything is already loaded. If load.js doesn't do it this way I am profoundly interested as to what the reasoning behind this might be ... perhaps all browser don't differ between window.load and document.ready? (or maybe I'm being stupid in some way or another)
- noibl 16y agodocument.ready fires before window.load
- weixiyen 16y agoWhat you are describing is not lazy-loading. The purpose of lazy loading is to avoid the overhead of having to load all your javascript immediately at the cost of more server requests later on as needed. The benefits of speed gained from lazy-loading can be huge in large applications with little to no browser refresh. The problem with lazy loading javascript in the browser is you have no good way innate to javascript to automatically tell if a script has really been loaded, and the problem you must solve is finding out when you are allowed to execute commands that have dependencies in the lazy loaded scripts.
- Swizec 16y agoThanks for the explanation, was almost certain I was thinking about it wrong :)
- bentlegen 16y agoProbably common knowledge, but RequireJS does this: http://requirejs.org/ http://requirejs.org/
- tlrobinson 16y agoRequireJS uses something similar to CommonJS modules, but they are not CommonJS modules. They use a "wrapped" format that makes it easier to load using script tags in browsers.
- rorrr 16y agoJS to load other JS never made sense to me.
- noodle 16y agojust thought i'd note (since you're new-ish) that you're getting downvoted because you stated an opinion without backing it up with any information. why doesn't it make sense to you? it makes sense to others.
- weixiyen 16y agoHave you ever worked in large scale applications with little to no browser refresh?
- Pewpewarrows 16y agoFirst off, you shouldn't be down-voted for your opinion. If you agree or the comment adds to a discussion, up-vote. If you disagree, do nothing. If the comment is off-topic, asinine, or detracts from the discussion, only then down-vote. Come on HN, you should know better. There's a lot of reasons to load Javascript from Javascript. One of the main ones that crop up is when you're building large single-page web applications that involve a lot of files that may or may not be dependent on one another. This allows you to start executing some of your page-specific Javascript as soon as the libraries it depends on become available, before the rest of your bulky resources finish loading.
- juddlyon 16y agoLab.js is also worth checking for script loading.
- Pewpewarrows 16y agoThis looks nice, but what does it offer that RequireJS and LABjs don't?
- sjs 16y agoI'd use RequireJS or LABjs before this, however it's based on a "microframework" called chain.js which looks pretty cool.