Appending an External JavaScript File.

12.27.2009

This basic bit of JavaScript will allow you to dynamically append an external JavaScript file to an HTML document.

var theScript = document.createElement('script'); theScript.type = 'text/javascript'; theScript.src = "da_script.js"; document.getElementsByTagName('head')[0].appendChild(theScript);

First, we prepare a new object using createElement(). Then, we append the new object to the end of the first (and, hopefully, only) head element in the document.

Permalink

Tags: javascript web-design

Authorized users may log-in to leave a comment.

Last Blog: Ye-ouch!

Next Blog: The Paranoid Web Developer.