Pew Pew Laser Blog

Code. Glass art. Games. Baking. Cats. From Seattle, Washington and various sundry satellite locations.

Pew Pew Laser Blog Archives — by Blog ID

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: code javascript

Authorized users may log-in to leave a comment.

Last Blog: Ye-ouch!

Next Blog: The Paranoid Web Developer.