LazyLoad

LazyLoad makes it easy and painless to lazily load one or more JavaScript files on demand after a web page has been rendered.

Supported browsers include Firefox 2.x, Firefox 3.x, Internet Explorer 6.x, Internet Explorer 7.x, Safari 3.x (including iPhone), and Opera 9.x.  Other browsers may or may not work and are not officially supported.

Author

Ryan Grove (ry.nosp@m.an@wonk.nosp@m.o.com)

Copyright

Copyright © 2008 Ryan Grove (ry.nosp@m.an@wonk.nosp@m.o.com).  All rights reserved.

License

BSD License (http://www.opensource.org/licenses/bsd-license.html)

URL

http://wonko.com/post/painless_javascript_lazy_loading_with_lazyload

Version

1.0.4 (2008-07-24)

Summary
LazyLoadLazyLoad makes it easy and painless to lazily load one or more JavaScript files on demand after a web page has been rendered.
Private Variables
dShorthand reference to the browser’s document object.
pendingPending request object, or null if no request is in progress.
queueArray of queued load requests.
uaUser agent information.
Private Methods
getUserAgentPopulates the ua variable with user agent information.
Public Methods
loadLoads the specified script(s) and runs the specified callback function when all scripts have been completely loaded.
loadOnceLoads the specified script(s) only if they haven’t already been loaded and runs the specified callback function when loading is complete.
requestCompleteHandles callback execution and cleanup after a request is completed.

Private Variables

d

var d

Shorthand reference to the browser’s document object.

pending

pending

Pending request object, or null if no request is in progress.

queue

queue

Array of queued load requests.

ua

ua

User agent information.

Private Methods

getUserAgent

function getUserAgent()

Populates the ua variable with user agent information.  Uses a paraphrased version of the YUI user agent detection code.

Public Methods

load

load: function (urls,
callback,
obj,
scope)

Loads the specified script(s) and runs the specified callback function when all scripts have been completely loaded.

Parameters

urlsURL or array of URLs of scripts to load
callbackfunction to call when loading is complete
obj(optional) object to pass to the callback function
scope(optional) if true, callback will be executed in the scope of obj instead of receiving obj as an argument.

loadOnce

loadOnce: function (urls,
callback,
obj,
scope,
force)

Loads the specified script(s) only if they haven’t already been loaded and runs the specified callback function when loading is complete.  If all of the specified scripts have already been loaded, the callback function will not be executed unless the force parameter is set to true.

Parameters

urlsURL or array of URLs of scripts to load
callbackfunction to call when loading is complete
obj(optional) object to pass to the callback function
scope(optional) if true, callback will be executed in the scope of obj instead of receiving obj as an argument
force(optional) if true, callback will always be executed, even if all specified scripts have already been loaded

requestComplete

requestComplete: function ()

Handles callback execution and cleanup after a request is completed.  This method should not be called manually.

var d
Shorthand reference to the browser’s document object.
pending
Pending request object, or null if no request is in progress.
queue
Array of queued load requests.
ua
User agent information.
function getUserAgent()
Populates the ua variable with user agent information.
load: function (urls,
callback,
obj,
scope)
Loads the specified script(s) and runs the specified callback function when all scripts have been completely loaded.
loadOnce: function (urls,
callback,
obj,
scope,
force)
Loads the specified script(s) only if they haven’t already been loaded and runs the specified callback function when loading is complete.
requestComplete: function ()
Handles callback execution and cleanup after a request is completed.
Close