Last Updated: February 25, 2016
·
1.105K
· matt6805

Write script tags to the document head

Here's a quick function that I've used lots of times to write script tags to the head of a document without the use of libraries:

function appendScriptTag(path) {
var head = document.getElementsByTagName('head')[0],
    script = document.createElement('script');
script.type = 'text/javascript';
script.src = path;
head.appendChild(script);
}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside