Get Class
Provides dynamic loading of remote JavaScript and CSS resources.
Item Index
Methods
Properties
- _env static
- _insertCache static
- _pending static
- _purgeNodes static
- _queue static
- cssOptions static
- jsOptions static
- options static
- REGEX_CSS static
- REGEX_JS static
Methods
_autoPurge
-
threshold
Triggers an automatic purge if the purge threshold has been reached.
Parameters:
-
threshold
NumberPurge threshold to use, in milliseconds.
_getEnv
()
Object
protected
static
Populates the _env
property with information about the current
environment.
Returns:
abort
-
transaction
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:
-
transaction
Get.TransactionTransaction to abort.
css
-
urls
-
[options]
-
[callback]
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 | ArrayURL string, request object, or array of URLs and/or request objects to load.
-
[options]
Object optionalOptions for this transaction. See the
Y.Get.options
property for a complete list of available options. -
[callback]
Function optionalCallback 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 theoptions
object.-
err
Array | NullArray of errors that occurred during the transaction, or
null
on success. -
transaction
Get.TransactionTransaction object.
-
Returns:
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]
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 | ArrayURL string, request object, or array of URLs and/or request objects to load.
-
[options]
Object optionalOptions for this transaction. See the
Y.Get.options
property for a complete list of available options. -
[callback]
Function optionalCallback 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 theoptions
object.-
err
Array | NullArray of errors that occurred during the transaction, or
null
on success. -
transaction
Get.TransactionTransaction object.
-
Returns:
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
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 | ArrayURL string, request object, or array of URLs and/or request objects to load.
-
[options]
Object optionalOptions for this transaction. See the
Y.Get.options
property for a complete list of available options. -
[callback]
Function optionalCallback 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 theoptions
object. -
err
Array | NullArray of errors that occurred during the transaction, or
null
on success. -
Transaction
Get.Transactionobject.
Returns:
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
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
Mapping of document _yuid strings to
or_pending
Object
protected
static
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
HTML nodes eligible to be purged next time autopurge is triggered.
cssOptions
Object
static
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
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
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 optionalWhether 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 optionalHTML 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-generatedid
attribute, but you can override these with your own values if desired. -
[autopurge]
Boolean optionalWhether or not to automatically purge inserted nodes after the purge threshold is reached. This is
true
by default for JavaScript, butfalse
for CSS since purging a CSS node will also remove any styling applied by the referenced file. -
[context]
Object optionalthis
object to use when calling callback functions. Defaults to the transaction object. -
[data]
Mixed optionalArbitrary data object to pass to "on*" callbacks.
-
[doc]
Document optionalDocument into which nodes should be inserted. By default, the current document is used.
-
[insertBefore]
HTMLElement | String optionalHTML 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 optionalCallback to execute after a transaction is complete, regardless of whether it succeeded or failed.
-
[onFailure]
Function optionalCallback to execute after a transaction fails, times out, or is aborted.
-
[onProgress]
Function optionalCallback to execute after each individual request in a transaction either succeeds or fails.
-
[onSuccess]
Function optionalCallback 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 optionalCallback to execute after a transaction times out.
-
[pollInterval=50]
Number optionalPolling 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 optionalNumber of nodes to insert before triggering an automatic purge when
autopurge
istrue
. -
[timeout]
Number optionalNumber of milliseconds to wait before aborting a transaction. When a timeout occurs, the
onTimeout
callback is called, followed byonFailure
and finallyonEnd
. By default, there is no timeout. -
[type]
String optionalResource type ("css" or "js"). This option is set automatically by the
css()
andjs()
functions and will be ignored there, but may be useful when using theload()
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
Regex that matches a CSS URL. Used to guess the file type when it's not specified.
REGEX_JS
RegExp
protected
final
static
Regex that matches a JS URL. Used to guess the file type when it's not specified.