1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 * @fileoverview A utility to load JavaScript files via DOM script tags.
17 * Refactored from goog.net.Jsonp. Works cross-domain.
21 goog.provide('goog.net.jsloader');
22 goog.provide('goog.net.jsloader.Error');
23 goog.provide('goog.net.jsloader.ErrorCode');
24 goog.provide('goog.net.jsloader.Options');
26 goog.require('goog.array');
27 goog.require('goog.async.Deferred');
28 goog.require('goog.debug.Error');
29 goog.require('goog.dom');
30 goog.require('goog.dom.TagName');
34 * The name of the property of goog.global under which the JavaScript
35 * verification object is stored by the loaded script.
39 goog.net.jsloader.GLOBAL_VERIFY_OBJS_ = 'closure_verification';
43 * The default length of time, in milliseconds, we are prepared to wait for a
44 * load request to complete.
47 goog.net.jsloader.DEFAULT_TIMEOUT = 5000;
51 * Optional parameters for goog.net.jsloader.send.
52 * timeout: The length of time, in milliseconds, we are prepared to wait
53 * for a load request to complete. Default it 5 seconds.
54 * document: The HTML document under which to load the JavaScript. Default is
55 * the current document.
56 * cleanupWhenDone: If true clean up the script tag after script completes to
57 * load. This is important if you just want to read data from the JavaScript
58 * and then throw it away. Default is false.
61 * timeout: (number|undefined),
62 * document: (HTMLDocument|undefined),
63 * cleanupWhenDone: (boolean|undefined)
66 goog.net.jsloader.Options;
70 * Scripts (URIs) waiting to be loaded.
71 * @type {Array.<string>}
74 goog.net.jsloader.scriptsToLoad_ = [];
78 * Loads and evaluates the JavaScript files at the specified URIs, guaranteeing
79 * the order of script loads.
81 * Because we have to load the scripts in serial (load script 1, exec script 1,
82 * load script 2, exec script 2, and so on), this will be slower than doing
83 * the network fetches in parallel.
85 * If you need to load a large number of scripts but dependency order doesn't
86 * matter, you should just call goog.net.jsloader.load N times.
88 * If you need to load a large number of scripts on the same domain,
89 * you may want to use goog.module.ModuleLoader.
91 * @param {Array.<string>} uris The URIs to load.
92 * @param {goog.net.jsloader.Options=} opt_options Optional parameters. See
93 * goog.net.jsloader.options documentation for details.
95 goog.net.jsloader.loadMany = function(uris, opt_options) {
96 // Loading the scripts in serial introduces asynchronosity into the flow.
97 // Therefore, there are race conditions where client A can kick off the load
98 // sequence for client B, even though client A's scripts haven't all been
101 // To work around this issue, all module loads share a queue.
106 var isAnotherModuleLoading = goog.net.jsloader.scriptsToLoad_.length;
107 goog.array.extend(goog.net.jsloader.scriptsToLoad_, uris);
108 if (isAnotherModuleLoading) {
109 // jsloader is still loading some other scripts.
110 // In order to prevent the race condition noted above, we just add
111 // these URIs to the end of the scripts' queue and return.
115 uris = goog.net.jsloader.scriptsToLoad_;
116 var popAndLoadNextScript = function() {
117 var uri = uris.shift();
118 var deferred = goog.net.jsloader.load(uri, opt_options);
120 deferred.addBoth(popAndLoadNextScript);
123 popAndLoadNextScript();
128 * Loads and evaluates a JavaScript file.
129 * When the script loads, a user callback is called.
130 * It is the client's responsibility to verify that the script ran successfully.
132 * @param {string} uri The URI of the JavaScript.
133 * @param {goog.net.jsloader.Options=} opt_options Optional parameters. See
134 * goog.net.jsloader.Options documentation for details.
135 * @return {!goog.async.Deferred} The deferred result, that may be used to add
136 * callbacks and/or cancel the transmission.
137 * The error callback will be called with a single goog.net.jsloader.Error
140 goog.net.jsloader.load = function(uri, opt_options) {
141 var options = opt_options || {};
142 var doc = options.document || document;
144 var script = goog.dom.createElement(goog.dom.TagName.SCRIPT);
145 var request = {script_: script, timeout_: undefined};
146 var deferred = new goog.async.Deferred(goog.net.jsloader.cancel_, request);
150 var timeoutDuration = goog.isDefAndNotNull(options.timeout) ?
151 options.timeout : goog.net.jsloader.DEFAULT_TIMEOUT;
152 if (timeoutDuration > 0) {
153 timeout = window.setTimeout(function() {
154 goog.net.jsloader.cleanup_(script, true);
155 deferred.errback(new goog.net.jsloader.Error(
156 goog.net.jsloader.ErrorCode.TIMEOUT,
157 'Timeout reached for loading script ' + uri));
159 request.timeout_ = timeout;
162 // Hang the user callback to be called when the script completes to load.
163 // NOTE(user): This callback will be called in IE even upon error. In any
164 // case it is the client's responsibility to verify that the script ran
166 script.onload = script.onreadystatechange = function() {
167 if (!script.readyState || script.readyState == 'loaded' ||
168 script.readyState == 'complete') {
169 var removeScriptNode = options.cleanupWhenDone || false;
170 goog.net.jsloader.cleanup_(script, removeScriptNode, timeout);
171 deferred.callback(null);
175 // Add an error callback.
176 // NOTE(user): Not supported in IE.
177 script.onerror = function() {
178 goog.net.jsloader.cleanup_(script, true, timeout);
179 deferred.errback(new goog.net.jsloader.Error(
180 goog.net.jsloader.ErrorCode.LOAD_ERROR,
181 'Error while loading script ' + uri));
184 // Add the script element to the document.
185 goog.dom.setProperties(script, {
186 'type': 'text/javascript',
188 // NOTE(user): Safari never loads the script if we don't set
189 // the src attribute before appending.
192 var scriptParent = goog.net.jsloader.getScriptParentElement_(doc);
193 scriptParent.appendChild(script);
200 * Loads a JavaScript file and verifies it was evaluated successfully, using a
201 * verification object.
202 * The verification object is set by the loaded JavaScript at the end of the
204 * We verify this object was set and return its value in the success callback.
205 * If the object is not defined we trigger an error callback.
207 * @param {string} uri The URI of the JavaScript.
208 * @param {string} verificationObjName The name of the verification object that
209 * the loaded script should set.
210 * @param {goog.net.jsloader.Options} options Optional parameters. See
211 * goog.net.jsloader.Options documentation for details.
212 * @return {!goog.async.Deferred} The deferred result, that may be used to add
213 * callbacks and/or cancel the transmission.
214 * The success callback will be called with a single parameter containing
215 * the value of the verification object.
216 * The error callback will be called with a single goog.net.jsloader.Error
219 goog.net.jsloader.loadAndVerify = function(uri, verificationObjName, options) {
220 // Define the global objects variable.
221 if (!goog.global[goog.net.jsloader.GLOBAL_VERIFY_OBJS_]) {
222 goog.global[goog.net.jsloader.GLOBAL_VERIFY_OBJS_] = {};
224 var verifyObjs = goog.global[goog.net.jsloader.GLOBAL_VERIFY_OBJS_];
226 // Verify that the expected object does not exist yet.
227 if (goog.isDef(verifyObjs[verificationObjName])) {
228 // TODO(user): Error or reset variable?
229 return goog.async.Deferred.fail(new goog.net.jsloader.Error(
230 goog.net.jsloader.ErrorCode.VERIFY_OBJECT_ALREADY_EXISTS,
231 'Verification object ' + verificationObjName + ' already defined.'));
234 // Send request to load the JavaScript.
235 var sendDeferred = goog.net.jsloader.load(uri, options);
237 // Create a deferred object wrapping the send result.
238 var deferred = new goog.async.Deferred(
239 goog.bind(sendDeferred.cancel, sendDeferred));
241 // Call user back with object that was set by the script.
242 sendDeferred.addCallback(function() {
243 var result = verifyObjs[verificationObjName];
244 if (goog.isDef(result)) {
245 deferred.callback(result);
246 delete verifyObjs[verificationObjName];
248 // Error: script was not loaded properly.
249 deferred.errback(new goog.net.jsloader.Error(
250 goog.net.jsloader.ErrorCode.VERIFY_ERROR,
251 'Script ' + uri + ' loaded, but verification object ' +
252 verificationObjName + ' was not defined.'));
256 // Pass error to new deferred object.
257 sendDeferred.addErrback(function(error) {
258 if (goog.isDef(verifyObjs[verificationObjName])) {
259 delete verifyObjs[verificationObjName];
261 deferred.errback(error);
269 * Gets the DOM element under which we should add new script elements.
270 * How? Take the first head element, and if not found take doc.documentElement,
271 * which always exists.
273 * @param {!HTMLDocument} doc The relevant document.
274 * @return {!Element} The script parent element.
277 goog.net.jsloader.getScriptParentElement_ = function(doc) {
278 var headElements = doc.getElementsByTagName(goog.dom.TagName.HEAD);
279 if (!headElements || goog.array.isEmpty(headElements)) {
280 return doc.documentElement;
282 return headElements[0];
288 * Cancels a given request.
289 * @this {{script_: Element, timeout_: number}} The request context.
292 goog.net.jsloader.cancel_ = function() {
294 if (request && request.script_) {
295 var scriptNode = request.script_;
296 if (scriptNode && scriptNode.tagName == 'SCRIPT') {
297 goog.net.jsloader.cleanup_(scriptNode, true, request.timeout_);
304 * Removes the script node and the timeout.
306 * @param {Node} scriptNode The node to be cleaned up.
307 * @param {boolean} removeScriptNode If true completely remove the script node.
308 * @param {?number=} opt_timeout The timeout handler to cleanup.
311 goog.net.jsloader.cleanup_ = function(scriptNode, removeScriptNode,
313 if (goog.isDefAndNotNull(opt_timeout)) {
314 goog.global.clearTimeout(opt_timeout);
317 scriptNode.onload = goog.nullFunction;
318 scriptNode.onerror = goog.nullFunction;
319 scriptNode.onreadystatechange = goog.nullFunction;
321 // Do this after a delay (removing the script node of a running script can
322 // confuse older IEs).
323 if (removeScriptNode) {
324 window.setTimeout(function() {
325 goog.dom.removeNode(scriptNode);
332 * Possible error codes for jsloader.
335 goog.net.jsloader.ErrorCode = {
339 VERIFY_OBJECT_ALREADY_EXISTS: 3
347 * @param {goog.net.jsloader.ErrorCode} code The error code.
348 * @param {string=} opt_message Additional message.
350 * @extends {goog.debug.Error}
353 goog.net.jsloader.Error = function(code, opt_message) {
354 var msg = 'Jsloader error (code #' + code + ')';
356 msg += ': ' + opt_message;
358 goog.net.jsloader.Error.base(this, 'constructor', msg);
361 * The code for this error.
363 * @type {goog.net.jsloader.ErrorCode}
367 goog.inherits(goog.net.jsloader.Error, goog.debug.Error);