Show:
Module: get

Provides dynamic loading of remote JavaScript and CSS resources.

Index

Methods

Properties

Methods

_autoPurge

(
  • threshold
)
protected static

Defined in yui3/src/get/js/get.js:490

Available since 3.5.0

Triggers an automatic purge if the purge threshold has been reached.

Parameters:

  • threshold Number

    Purge threshold to use, in milliseconds.

_getEnv

() Object protected static

Defined in yui3/src/get/js/get.js:506

Available since 3.5.0

Populates the _env property with information about the current environment.

Returns:

Object:

Environment information.

abort

(
  • transaction
)
deprecated static

Defined in yui3/src/get/js/get.js:240

Deprecated: Use the `abort()` method on the transaction instead.

Aborts the specified transaction.

This will cause the transaction's onFailure callback to be called and will prevent any new script and link nodes from being added to the document, but any resources that have already been requested will continue loading (there's no safe way to prevent this, unfortunately).

Note: This method is deprecated as of 3.5.0, and will be removed in a future version of YUI. Use the transaction-level abort() method instead.

Parameters:

css

(
  • urls
  • [options]
  • [callback]
)
Get.Transaction static

Loads one or more CSS files.

The urls parameter may be provided as a URL string, a request object, or an array of URL strings and/or request objects.

A request object is just an object that contains a url property and zero or more options that should apply specifically to that request. Request-specific options take priority over transaction-level options and default options.

URLs may be relative or absolute, and do not have to have the same origin as the current page.

The options parameter may be omitted completely and a callback passed in its place, if desired.

Parameters:

  • urls String | Object | Array

    URL string, request object, or array of URLs and/or request objects to load.

  • [options] Object optional

    Options for this transaction. See the Y.Get.options property for a complete list of available options.

  • [callback] Function optional

    Callback function to be called on completion. This is a general callback and will be called before any more granular callbacks (onSuccess, onFailure, etc.) specified in the options object.

    • err Array | Null

      Array of errors that occurred during the transaction, or null on success.

    • transaction Get.Transaction

      Transaction object.

Returns:

Get.Transaction:

Transaction object.

Example:

// Load a single CSS file and log a message on completion.
                                                Y.Get.css('foo.css', function (err) {
                                                    if (err) {
                                                        Y.log('foo.css failed to load!');
                                                    } else {
                                                        Y.log('foo.css was loaded successfully');
                                                    }
                                                });
                                                
                                                // Load multiple CSS files and log a message when all have finished
                                                // loading.
                                                var urls = ['foo.css', 'http://example.com/bar.css', 'baz/quux.css'];
                                                
                                                Y.Get.css(urls, function (err) {
                                                    if (err) {
                                                        Y.log('one or more files failed to load!');
                                                    } else {
                                                        Y.log('all files loaded successfully');
                                                    }
                                                });
                                                
                                                // Specify transaction-level options, which will apply to all requests
                                                // within the transaction.
                                                Y.Get.css(urls, {
                                                    attributes: {'class': 'my-css'},
                                                    timeout   : 5000
                                                });
                                                
                                                // Specify per-request options, which override transaction-level and
                                                // default options.
                                                Y.Get.css([
                                                    {url: 'foo.css', attributes: {id: 'foo'}},
                                                    {url: 'bar.css', attributes: {id: 'bar', charset: 'iso-8859-1'}}
                                                ]);

js

(
  • urls
  • [options]
  • [callback]
)
Get.Transaction static

Defined in yui3/src/get/js/get.js:360

Available since 3.5.0

Loads one or more JavaScript resources.

The urls parameter may be provided as a URL string, a request object, or an array of URL strings and/or request objects.

A request object is just an object that contains a url property and zero or more options that should apply specifically to that request. Request-specific options take priority over transaction-level options and default options.

URLs may be relative or absolute, and do not have to have the same origin as the current page.

The options parameter may be omitted completely and a callback passed in its place, if desired.

Scripts will be executed in the order they're specified unless the async option is true, in which case they'll be loaded in parallel and executed in whatever order they finish loading.

Parameters:

  • urls String | Object | Array

    URL string, request object, or array of URLs and/or request objects to load.

  • [options] Object optional

    Options for this transaction. See the Y.Get.options property for a complete list of available options.

  • [callback] Function optional

    Callback function to be called on completion. This is a general callback and will be called before any more granular callbacks (onSuccess, onFailure, etc.) specified in the options object.

    • err Array | Null

      Array of errors that occurred during the transaction, or null on success.

    • transaction Get.Transaction

      Transaction object.

Returns:

Get.Transaction:

Transaction object.

Example:

// Load a single JS file and log a message on completion.
                                                Y.Get.js('foo.js', function (err) {
                                                    if (err) {
                                                        Y.log('foo.js failed to load!');
                                                    } else {
                                                        Y.log('foo.js was loaded successfully');
                                                    }
                                                });
                                                
                                                // Load multiple JS files, execute them in order, and log a message when
                                                // all have finished loading.
                                                var urls = ['foo.js', 'http://example.com/bar.js', 'baz/quux.js'];
                                                
                                                Y.Get.js(urls, function (err) {
                                                    if (err) {
                                                        Y.log('one or more files failed to load!');
                                                    } else {
                                                        Y.log('all files loaded successfully');
                                                    }
                                                });
                                                
                                                // Specify transaction-level options, which will apply to all requests
                                                // within the transaction.
                                                Y.Get.js(urls, {
                                                    attributes: {'class': 'my-js'},
                                                    timeout   : 5000
                                                });
                                                
                                                // Specify per-request options, which override transaction-level and
                                                // default options.
                                                Y.Get.js([
                                                    {url: 'foo.js', attributes: {id: 'foo'}},
                                                    {url: 'bar.js', attributes: {id: 'bar', charset: 'iso-8859-1'}}
                                                ]);

load

(
  • urls
  • [options]
  • [callback]
  • err
  • Transaction
)
Get.Transaction static

Defined in yui3/src/get/js/get.js:440

Available since 3.5.0

Loads one or more CSS and/or JavaScript resources in the same transaction.

Use this method when you want to load both CSS and JavaScript in a single transaction and be notified when all requested URLs have finished loading, regardless of type.

Behavior and options are the same as for the css() and js() methods. If a resource type isn't specified in per-request options or transaction-level options, Get will guess the file type based on the URL's extension (.css or .js, with or without a following query string). If the file type can't be guessed from the URL, a warning will be logged and Get will assume the URL is a JavaScript resource.

Parameters:

  • urls String | Object | Array

    URL string, request object, or array of URLs and/or request objects to load.

  • [options] Object optional

    Options for this transaction. See the Y.Get.options property for a complete list of available options.

  • [callback] Function optional

    Callback function to be called on completion. This is a general callback and will be called before any more granular callbacks (onSuccess, onFailure, etc.) specified in the options object.

  • err Array | Null

    Array of errors that occurred during the transaction, or null on success.

  • Transaction Get.Transaction

    object.

Returns:

Get.Transaction:

Transaction object.

Example:

// Load both CSS and JS files in a single transaction, and log a message
                                                // when all files have finished loading.
                                                Y.Get.load(['foo.css', 'bar.js', 'baz.css'], function (err) {
                                                    if (err) {
                                                        Y.log('one or more files failed to load!');
                                                    } else {
                                                        Y.log('all files loaded successfully');
                                                    }
                                                });

script

() static

Alias for js().

Properties

_env

Object protected static

Defined in yui3/src/get/js/get.js:175

Available since 3.5.0

Contains information about the current environment, such as what script and link injection features it supports.

This object is created and populated the first time the _getEnv() method is called.

_insertCache

Object protected static

Defined in yui3/src/get/js/get.js:189

Available since 3.5.0

Mapping of document _yuid strings to <head> or <base> node references so we don't have to look the node up each time we want to insert a request node.

_pending

Object protected static

Defined in yui3/src/get/js/get.js:201

Available since 3.5.0

Information about the currently pending transaction, if any.

This is actually an object with two properties: callback, containing the optional callback passed to css(), load(), or js(); and transaction, containing the actual transaction instance.

_purgeNodes

HTMLElement protected static

Defined in yui3/src/get/js/get.js:216

Available since 3.5.0

HTML nodes eligible to be purged next time autopurge is triggered.

_queue

Object protected static

Defined in yui3/src/get/js/get.js:227

Available since 3.5.0

Queued transactions and associated callbacks.

cssOptions

Object static

Defined in yui3/src/get/js/get.js:20

Available since 3.5.0

Default options for CSS requests. Options specified here will override global defaults for CSS requests.

See the options property for all available options.

jsOptions

Object static

Defined in yui3/src/get/js/get.js:40

Available since 3.5.0

Default options for JS requests. Options specified here will override global defaults for JS requests.

See the options property for all available options.

options

Object static

Defined in yui3/src/get/js/get.js:56

Available since 3.5.0

Default options to use for all requests.

Note that while all available options are documented here for ease of discovery, some options (like callback functions) only make sense at the transaction level.

Callback functions specified via the options object or the options parameter of the css(), js(), or load() methods will receive the transaction object as a parameter. See Y.Get.Transaction for details on the properties and methods available on transactions.

Sub-properties:

  • [async=false] Boolean optional

    Whether or not to load scripts asynchronously, meaning they're requested in parallel and execution order is not guaranteed. Has no effect on CSS, since CSS is always loaded asynchronously.

  • [attributes] Object optional

    HTML attribute name/value pairs that should be added to inserted nodes. By default, the charset attribute will be set to "utf-8" and nodes will be given an auto-generated id attribute, but you can override these with your own values if desired.

  • [autopurge] Boolean optional

    Whether or not to automatically purge inserted nodes after the purge threshold is reached. This is true by default for JavaScript, but false for CSS since purging a CSS node will also remove any styling applied by the referenced file.

  • [context] Object optional

    this object to use when calling callback functions. Defaults to the transaction object.

  • [data] Mixed optional

    Arbitrary data object to pass to "on*" callbacks.

  • [doc] Document optional

    Document into which nodes should be inserted. By default, the current document is used.

  • [insertBefore] HTMLElement | String optional

    HTML element or id string of an element before which all generated nodes should be inserted. If not specified, Get will automatically determine the best place to insert nodes for maximum compatibility.

  • [onEnd] Function optional

    Callback to execute after a transaction is complete, regardless of whether it succeeded or failed.

  • [onFailure] Function optional

    Callback to execute after a transaction fails, times out, or is aborted.

  • [onProgress] Function optional

    Callback to execute after each individual request in a transaction either succeeds or fails.

  • [onSuccess] Function optional

    Callback to execute after a transaction completes successfully with no errors. Note that in browsers that don't support the error event on CSS <link> nodes, a failed CSS request may still be reported as a success because in these browsers it can be difficult or impossible to distinguish between success and failure for CSS resources.

  • [onTimeout] Function optional

    Callback to execute after a transaction times out.

  • [pollInterval=50] Number optional

    Polling interval (in milliseconds) for detecting CSS load completion in browsers that don't support the load event on <link> nodes. This isn't used for JavaScript.

  • [purgethreshold=20] Number optional

    Number of nodes to insert before triggering an automatic purge when autopurge is true.

  • [timeout] Number optional

    Number of milliseconds to wait before aborting a transaction. When a timeout occurs, the onTimeout callback is called, followed by onFailure and finally onEnd. By default, there is no timeout.

  • [type] String optional

    Resource type ("css" or "js"). This option is set automatically by the css() and js() functions and will be ignored there, but may be useful when using the load() function. If not specified, the type will be inferred from the URL, defaulting to "js" if the URL doesn't contain a recognizable file extension.

REGEX_CSS

RegExp protected final static

Defined in yui3/src/get/js/get.js:149

Available since 3.5.0

Regex that matches a CSS URL. Used to guess the file type when it's not specified.

REGEX_JS

RegExp protected final static

Defined in yui3/src/get/js/get.js:162

Available since 3.5.0

Regex that matches a JS URL. Used to guess the file type when it's not specified.