- Forums
- Javascipt
- Example Of Javascript Lazy Load Function
this is an example function you can use to lazy load all of your javascript scripts to load after you web page has finised loading. [4569], Last Updated: Mon Jun 24, 2024
gili
Mon Aug 17, 2015
0 Comments
519 Visits
i use this lazy load function to load all my javascript faster.
<script>
(function() {
function LazyLoad(script, callback) {
var se = document.createElement('script'); se.src = script; se.async = true;
if (callback) {
se.onload = se.onreadystatechange = function() {
if (!(this.readyState && this.readyState !== 'complete' && this.readyState !== 'loaded')) {
se.onload = se.onreadystatechange = null;
callback();
}
};
}
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(se, s);
}
LazyLoad('//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {
LazyLoad('//celebdial.storage.googleapis.com/static/js/jquery.pjax.min.js');
LazyLoad('//celebdial.storage.googleapis.com/static/js/bootstrap.min.js');
LazyLoad('//celebdial.storage.googleapis.com/static/celebcall/js/celebcall.min.js');
});
})();
</script>