Dynamically add script tag with src that may include document.write

I want to dynamically include a script tag in a webpage however I have no control of it’s src so src=”source.js” may look like this.

document.write('<script type="text/javascript">')
document.write('alert("hello world")')
document.write('</script>')
document.write('<p>goodbye world</p>')

Now ordinarily putting

<script type="text/javascript" src="source.js"></script>

In the head works fine but is there any other way I can add source.js dynamically using something like innerHTML?

jsfiddle of what i’ve tried

16 Answers
16

Leave a Comment