Skip to content
Eugene Lazutkin edited this page Nov 26, 2019 · 2 revisions

load() is a helper to include JavaScript files dynamically based on the general infrastructure of io().

Don't forget to consult the cookbook to see working snippets solving simple real-world problems.

The main API

io.load() is modelled on io.get(). It supports the same arguments and returns a promise.

io.load(url|options, [query])

io.load() creates a script element. It takes two arguments:

  • url is a URL as a string.
  • options is an options object described above (see io()).
  • query is an optional dictionary to form a query string (see query above in io()).

Either url or options should be specified.

The returned value is a promise (see io() for details).

Differences with io()

io.load() does not return any value, so a promise will be resolved with undefined.

One obvious difference is the lack of an XHR object. Everywhere it is expected, null is used.

Following helper methods, which are defined in io are not used:

  • io.processData() — we cannot send any payload.
  • io.request() — we do not use XHR.
  • io.processSuccess() — we don't need to extract data from XHR using different formats, because it is already provided.

Because io.load() does not participate in the main io workflow, it is not affected by high-level tools like io.bundle.

Certain optional properties of options are ignored completely:

  • method — only one method is supported: GET.
  • headers — there is no way to define or change HTTP headers.
  • user and password — unless it is encoded in a URL, a script element cannot pass a user name.
  • timeout, responseType, withCredentials, mime — XHR-specific settings cannot be used.
  • wait, cache, bundle — all advanced tools are bypassed.
  • processSuccess — this function extracts data from XHR, while a script does not return any.

While an XHR request can be canceled, a script request cannot. No progress requests are sent out either.

Clone this wiki locally