Skip to content

Instantly share code, notes, and snippets.

@elycruz
Last active August 29, 2015 14:18
Show Gist options
  • Save elycruz/9eb3657704daa38c3fd9 to your computer and use it in GitHub Desktop.
Save elycruz/9eb3657704daa38c3fd9 to your computer and use it in GitHub Desktop.
Append a script tag (legacy way with optional use of jquery)
/**
* Creates and appends a script tag to the `document` at `insertLocation`. (This snippet is a good candidate for adding an
* interval or a timeout on the returned $.deffered|HTMLScriptElement)
* @param options hash of the following parameters:
* @param src {String|HTMLScriptElement} - Script source or script element. Required.
* @param doc {document} - `document` object. Optional. Default `document`.
* @param insertLocation {String} - Location at which to insert the script tag [head,body]. Optional. Default 'body'.
* @param onError {Function} - On error callback. Optional. If `$.getScript` is available these callbacks gets passed to `.fail()`.
* @param onSuccess {Function} - On success/completion callback. Optional.
* @param useGetScript {Boolean} - Whether to use `$.getScript` if it is available. Optional. Default `false`.
* **Note If passing true for this parameter then only a `String` `src` parameter will be allowed.
* @param logging {Boolean} - Control variable to give the user control from his/her callbacks. Optional. Used by internal 'no-op' function.
* ** To use defaults just pass in `null` for the values you do not want to use (except for the required values).
* ** Note default logging is false. If it is set to `true` without passing any callbacks (onSuccess, onError)
* the error object is logged to the console.
* @return {mixed|$.deffered|HTMLScriptElement} - If `$.getScript` is not used (see @param comments above) returns
* HTMLcriptElement else it returns `$.deffered`.
*/
function appendScriptTag(options) {
// Prelims
var isGetScriptAvailable = typeof $ !== 'undefined' && sjl.classOfIs($.getScript, 'Function'),
scriptTag,
retVal = null,
noopCallback = function (e) { if(options.logging) {console.log(e);}},
src, error, success, doc;
options = sjl.extend({
doc: document,
logging: false,
insertLocation: 'body',
useGetScript: false,
error: noopCallback,
success: noopCallback,
src: null
}, options);
doc = options.doc;
src = options.src;
error = options.error;
success = options.success;
// If no `$.getScript` append 'script' tag with vanilla (plain) javascript
if (!options.useGetScript || !isGetScriptAvailable) {
// Create script element if none was passed
if (sjl.classOfIs(src, 'String')) {
scriptTag = doc.createElement('script');
scriptTag.src = src;
scriptTag.onerror = error;
scriptTag.onload = success;
}
// Else assume `src` is a script element
else {
scriptTag = src;
}
// Insert script tag
doc[options.insertLocation].appendChild(scriptTag);
// Exit
retVal = scriptTag;
}
// Append script tag and call callbacks
return retVal === null ? $.getScript(src, success).fail(error) : retVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment