Module: rgjs/dom-load

RGJS6 DOM module.

NOTE: This module is exported as part of rgjs.dom.

Methods


<static> abort(requestId [, cleanUp])

Abort a request.

Parameters:
Name Type Argument Default Description
requestId string

The request id returned by loadScript(), loadStyle(), etc.

cleanUp boolean <optional>
true

Also cleans up request if true. Defaults to true.

Returns:

True if the request was aborted & cleaned up, false if it wasn't found.

Type
boolean
Examples
// Use a generated id
const reqId = loadHtml('html/some-snippet.html', function() { done(); }, function() { retry(); });
abort(reqId);
// Provide an id
const reqId = loadHtml('html/some-snippet.html', function() { done(); }, function() { retry(); }, {id: 'my-unique-id'});
abort('my-unique-id'); // use the id directly
abort(reqId); // use the return value (in this example, `reqId = 'my-unique-id'`)

<static> abortAll()

Abort all requests.

Example
loadScript('js/some-script.js', function() { done(); }, function() { retry(); });
loadStyle('css/some-style.css', function() { done(); }, function() { retry(); });
loadHtml('html/some-snippet.html', function() { done(); }, function() { retry(); });
abortAll();

<static> loadHtml(url [, cb] [, cberr] [, opts])

Load html and append it to the dom.

Parameters:
Name Type Argument Description
url string

The url of the script.

cb function <optional>

The callback. Arguments: response, status, statusText, xhr.

cberr function <optional>

The error callback. Arguments: status, statusText, xhr.

opts object <optional>

Options are optional.

Properties
Name Type Argument Default Description
id string <optional>
null

Identifier for request. Used to abort ongoing requests. Generated automatially if not set.

target string <optional>
'body'

A selector or target element to target.

targetMode string <optional>
'append'

Mode used to add content in relation to target. Values: append, replace, data.

Returns:

An identifier that can be used to abort the request.

Type
string
Example
loadHtml('html/some-snippet.html', function() { done(); }, function() { retry(); });

<static> loadList(list [, cb] [, cberr])

Load a bunch of js, css, html.
Loads one by one. Can not be aborted.

Parameters:
Name Type Argument Description
list array

A list of things to load: [{type, url, cb, cberr, opts}].

cb function <optional>

Callback.

cberr function <optional>

Callback for errors.

Example
// Loads a list of files, then calls done().
const list = [
  {type: 'script', url: './js/script.js', opts: {attrs: {id: 'script'}}},
  {type: 'style', url: './css/style.css', opts: {attrs: {id: 'style'}}},
  {type: 'html', './html/snippet.html'},
];
loadList(list, function() { done(); }, function() { retry(); });

<static> loadListAsync(list [, cb] [, cberr])

Load a bunch of js, css, html.
Loads in async/parallel if Promises are supported (falls back to loadList). Can not be aborted.

Parameters:
Name Type Argument Description
list array

A list of things to load: [{type, url, cb, cberr, opts}].

cb function <optional>

Callback.

cberr function <optional>

Callback for errors.

Example
// Loads a list of files, then calls done().
const list = [
  {type: 'script', url: './js/script.js', opts: {attrs: {id: 'script'}}},
  {type: 'style', url: './css/style.css', opts: {attrs: {id: 'style'}}},
  {type: 'html', './html/snippet.html'},
];
loadListAsync(list, function() { done(); }, function() { retry(); });

<static> loadScript(url [, cb] [, cberr] [, opts])

Load javascript by appending a <script> tag to the DOM.

Parameters:
Name Type Argument Description
url string

The url of the script.

cb function <optional>

The callback.

cberr function <optional>

The error callback.

opts object <optional>

Options are optional.

Properties
Name Type Argument Default Description
parentSelector string <optional>
'body'

The element that gets the <script> tag appended to it.

async boolean <optional>
true

Sets the async attribute. Note: will be true in almost any scenario due to browser implementation.

attrs object <optional>
{}

Optional attributes. Example: {id: 'script'} will result in <script id="script">

Returns:

An identifier that can be used to abort the request (removes the script tag).

Type
string
Example
loadScript('js/some-script.js', function() { done(); }, function() { retry(); });

<static> loadStyle(url [, cb] [, cberr] [, opts])

Load css by appending a <link> tag to the dom.

Parameters:
Name Type Argument Description
url string

The url of the stylesheet.

cb function <optional>

The callback.

cberr function <optional>

The error callback. Note: may not work in all browsers.

opts object <optional>

Options are optional.

Properties
Name Type Argument Default Description
parentSelector string <optional>
'body'

The element that gets the <link> tag appended to it.

async boolean <optional>
true

Sets the async and lazyload attribute.

attrs object <optional>
{}

Optional attributes. Example: {id: 'script'} will result in <link id="script">

Returns:

An identifier that can be used to abort the request (removes the link tag).

Type
string
Example
loadStyle('css/some-style.css', function() { done(); }, function() { retry(); });