Skip to content

Instantly share code, notes, and snippets.

@stvrbbns
Created January 1, 2016 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stvrbbns/9e87e71f4f09734d0eef to your computer and use it in GitHub Desktop.
Save stvrbbns/9e87e71f4f09734d0eef to your computer and use it in GitHub Desktop.
/** includeJS() appends a JavaScript file to an element of the open document.
* @param jsPath -- string - the path to the JavaScript file.
* @param docLocName -- string - optional - element tag or ID name;
* defaults to "head".
* @param docLocType -- string - optional - type of element name, "tag" or "id";
* defaults to "tag".
* @param docLocNum -- integer - optional - number of element with tag name;
* defaults to 0.
*/
function includeJS(jsPath, docLocName, docLocType, docLocNum)
{
// Set default values.
if(docLocName == null) { docLocName = 'head'; }
if(docLocType == null) { docLocType = 'tag'; }
if(docLocNum == null) { docLocNum = 0; }
// Prepare the JavaScript document.
var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', jsPath);
// Append the JavaScript document to the appropriate document location.
if(docLocType == 'tag')
{
document.getElementsByTagName(docLocName)[docLocNum].appendChild(js);
} else if(docLocType == 'id')
{
document.getElementsById(docLocName).appendChild(js);
}
} // end of includeJS() function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment