script.js icon indicating copy to clipboard operation
script.js copied to clipboard

Javascript lazy loading

Open xorock opened this issue 14 years ago • 5 comments

What would you say to: $script.lazy('jquery-latest.min', 'jquery'); //just define a handler

//load when script is requested $script.ready('jquery'....

Is it hard to implement?

xorock avatar Sep 28 '11 09:09 xorock

nope. it wouldn't be difficult at all. and script.js should in fact be able to support the ability to say $script.ready('jquery', fn) without having to define it first. Thus whenever you're ready to handle the script, you would just fire it off with $script('jquery.min.js', 'jquery')

ded avatar Sep 28 '11 17:09 ded

but in other words I'll look into it

ded avatar Sep 28 '11 17:09 ded

Idea is to have one dependency global file where I can define all js modules and then just load them without worry what version it is. That is also alternative to , function(depsNotFound) { // foo.js & bar.js may have downloaded // but ['thunk'] dependency was never found // so lazy load it now depsNotFound.forEach(function(dep) { $script(dependencyList[dep], dep) }) Thank You :)

xorock avatar Sep 29 '11 04:09 xorock

@xorock array.forEach is not supported in IE 6-8. Use var i = depsNotFound.length; while(i--) { ... } https://github.com/ded/script.js/pull/48

willfarrell avatar Apr 01 '13 18:04 willfarrell

I'd love this. I came here looking for the same idea. I want to define my dependency chain in $script, AND, incase of other scripts load first, define ready() before the id is defined.

Or another way to say it: I want to retrieve scripts without executing them. Or, alternatively, retrieve and execute only when they're explicitly asked for, resolving all of their dependencies.

matthew-dean avatar May 02 '15 18:05 matthew-dean