The Get Utility provides a mechanism for attaching script and css resources — including cross-domain resources — to the DOM after the page has loaded.
There are two common use cases for the Get Utility:
The Get Utility inserts new content into a window by creating new nodes and supplying a src attribute. Files inserted into the window in this manner are processed (and, in the case of scripts, executed) immediately upon load. While querystring parameters can be passed in the src attribute URL, no data can be sent to the server via HTTP POST using this method. The Get Utility can only make HTTP GET requests. It can, however, interact with disparate domains. As noted above, the Get Utility is ideal for loading your own scripts or CSS progressively (lazy-loading) or for retrieving cross-domain JSON data from sources in which you have total trust.
The YUI Connection Manager, by contrast, uses the XMLHttpRequest object to interact with the server. XMLHttpRequest is limited by a strict same origin policy, but it supports a greater range of HTTP methods (including POST). Moreover, script data retrieved via XMLHttpRequest can be validated on the server side or in JavaScript (using, for example, the JSON Utility) prior to being executed. As a result, Connection Manager is a more appropriate choice for rich two-way communication between browser and server and gives you more control over data before it's processed within the browser.
The Get Utility's only dependency is the Yahoo Global Object. To use the Get Utility, include the following source files in your web page:
Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader write out the full HTML for including the precise files you need for your implementation.
Note: If you are loading components via the YUI Loader, this component is included in the YUI Loader package — you do not need to load it separately. If YUI Loader is on the page, so is the Get Utility.
Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.
Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.
With the Get Utility present, you can make use of it to fetch script (YAHOO.util.Get.script()) and/or CSS (YAHOO.util.Get.css()) resources to the page. The script and css methods each take the following arguments:
A sample request for a file might look like this:
The Get Utility is configured via the second argument to the script or css method. This optional argument comprises an object containing one or more of the following fields:
| Configuration Option | Purpose |
|---|---|
| onSuccess | (function) Callback method invoked by Get Utility when the requested file(s) have loaded successfully. |
| onFailure | (function) Callback method invoked by Get Utility when an error is detected or abort is called. |
| win | (obj) The window into which the loaded resource(s) will be inserted. Default: the current window. |
| scope | (object) The execution scope in which the onSuccess or onFailure callback will run. Default: the current window. |
| data | (any) Data to pass as an argument to onSuccess or onFailure callbacks. Default: null. |
| varName | (array of strings) Safari 2.x does not reliably report the load-complete status of script nodes. Use this property to provide the Get Utility with a globally accessible property that will be available when the script has loaded; the Get Utility will poll the global namespace for this property and not fire your callback or load subsequent scripts until the property is present. This array is parallel to the urls array passed in as the first argument to script() — each varName corresponds to a single script being loaded. When you are dealing with loading and execution steps that need to be synchronous, this property is required for reliable performance in Safari 2.x, especially on slow connections. |
| autopurge | (boolean) If set to true, script nodes will automatically be removed every 20 transactions (this number is globally configurable via the YAHOO.util.Get.PURGE_THRESH property); script nodes can be safely removed in most cases, as their contents (having executed) remain available. CSS nodes should not have this set to true as it will remove the CSS rules. Default: true for script nodes, false for CSS nodes. |
As noted in the section above, your callback method (whether onSuccess or onFailure) will have access to the data member supplied in the configuration object, assuming you provided one. But the data member is just one of several fields included in the object passed to your callback. Here's a summary of the fields contained in that argument object:
| Field | Contents |
|---|---|
| tId | (string) The unique identifier for this transaction; this string is available as the tId member of the object returned to you upon calling the script or css method. |
| data | (any) The data field you passed in your configuration object when the script or css method was called. Default: null. |
| nodes | (array) An array containing references to node(s) created in processing the transaction. These will be script nodes for JavaScript and link nodes for CSS. |
| win | (obj) The window object in which the nodes were created. |
| purge() | (function) Calling the returned purge() method will immediately remove the created nodes. This is safe and prudent for JavaScript nodes, which do not need to persist. If CSS nodes are purged, the rules they contain are no longer available and the page will repaint accordingly. |
All of these fields are accessible on the object passed to your onSuccess callback:
When you use the YAHOO.util.Get.script() method to add one or more script nodes to the page, the Get Utility implements the following steps:
window for the first script file requested;src attribute of the new script node is set to the specified URL;
load event fires;onSuccess callback (if one has been specified);The following is the general code pattern used to get scripts:
When you use the YAHOO.util.Get.css() method to add one or more CSS files to the page, the Get Utility follows the same steps described above for scripts. Generally speaking, the same code pattern holds as well. Note that Firefox at present does not support the load event on link nodes, so it is possible for CSS requests to load out of order in that browser. This can result in a different progressive display of styled in Firefox versus other browsers during CSS loading.
About this Section: YUI generally works well with mobile browsers that are based on A-Grade browser foundations. For example, Nokia's N-series phones, including the N95, use a browser based on Webkit — the same foundation shared by Apple's Safari browser, which is found on the iPhone. The fundamental challenges in developing for this emerging class of full, A-Grade-derived browsers on handheld devices are:
There are other considerations, many of them device/browser specific (for example, current versions of the iPhone's Safari browser do not support Flash). The goal of these sections on YUI User's Guides is to provide you some preliminary insights about how specific components perform on this emerging class of mobile devices. Although we have not done exhaustive testing, and although these browsers are revving quickly and present a moving target, our goal is to provide some early, provisional advice to help you get started as you contemplate how your YUI-based application will render in the mobile world.
More Information:
The Get Utility is fully supported on Apple's iPhone and the Nokia N-Series browsers; you can expect functional parity between their implementation and those on the supported A-Grade browsers.
The YUI Library and related topics are discussed on the on the ydn-javascript mailing list.
In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.
The YUI Library's public bug tracking and feature request repositories are located on the YUI SourceForge project site. Before filing new feature requests or bug reports, please review our reporting guidelines.














Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings