0

Possible Duplicate:
How do you dynamically load a javascript file? (Think C's #include)
Include JavaScript file inside JavaScript file?

I know that one can link to external javascript files with html code akin to:

<script type="text/javascript" src="http://external.com/code.js"></script>

Is there a way to do this within a javascript (.js) file?

2

2 Answers 2

3

Create a script element and append it to the DOM. That should load your other script.

var newScript = document.createElement("script");
newScript.setAttribute("type","text/javascript");
newScript.setAttribute("src","http://external.com/code.js");
body.appendChild(newScript);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I don't think it's working for my example: jsfiddle.net/V9jdu
Note that in HTML5 the type attribute is not used.
1

You could try the answer given in this question.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.