# DemandJS [![logo](logo_transparent.png)](https://github.com/hannasm/demandjs) ## Version 1.0.0-rc.14 - we have reached a point where the intended features of this project are there. It is still unproven in production and requires some adoption and time to mature before it can be considered a stable release. ## Acknowledgement If you find this project useful and would like to make a contribution you may do so here. [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=hannasm%40gmail.com&item_name=demandjs¤cy_code=USD) I am available for consulting, feedback and other opportunities, conact me at: [Resonance Development](https://rsndev.com/resume) ## Overview DemandJS provides lazy loading of resources as they scroll into view using true progressive enhancement and the latest browser features. DemandJS works with most elements using a src attribute including images, videos, and there is extra support for embedding external content of all types, and especially html files. DemandJS uses the latest (2017) of modern browser features and aims to be as efficient and lightweight as possible. For older browser compatibility, you can polyfill features to reach your target audience. Loading resources on demand can drastically reduce initial page load times and perceived page performance. ## Installation The minified and optimized, release version of this library is checked into the repository as [demandjs.min.js](https://raw.githubusercontent.com/hannasm/demandjs/master/demandjs.min.js). For specific versions you can also check the [Release Page](https://github.com/hannasm/demandjs/releases) There are no dependencies however refer to the polyfill section below for some specific compaibility considerations. ## Best Practices * It is important that your loading elements are approximately the same dimensions as your actual content. DemandJS replaces your actual content with loading elements (e.g. loading anmiation). When the loading element becomes visible on the page, demandjs will begin loading your actual content. If the loading element is significantly smaller than the actual content, and several are clumped together in the layout, loading may start earlier than was actually needed. If your loading elements are significantly larger than your actual content, and several are clumped together, inserting your actual content into the page may lead to other loading elements popping into view, and hence additional loads happening later than intended. * Dont use styles directly on your media elements to control visibilty. Any styles applied directly to your media elements will not also be applied to the loading and failure ui. The intention of the loading ui in particular is to identify when the user can see the media and when loading needs to begin. If you are directly styling your media elements to hide them loading will probably begin prematurely (because the loading ui is visible), and loading graphics will be visible in the page when they shouldn't be. * In the default configuration, demandjs will ignore any elements with the 'nodemand' class. You may also use the ```ignoreSelector``` option to customize which elements are ignored. ([see test020](test/test020.html)) * Use the ```previewLoading:true``` and ```previewFailure:true``` options during testing / development to make sure that your loading and failure ui looks good and works the way you want it those ## Usage To get up and running with demandJS you simply import the file into your page and instantiate it: ```javascript window.DemandJSDemanded = new DemandJS(); ``` * ([see test001](test/test001.html)) * ([see test002](test/test002.html)) the default options will capture all img, video, picture, iframe in the page, cancel anything that may have been loading already, replace those elements with a boilerplate loading text, and then load those resources when they are actually needed ```html