2

I need to include a JavaScript file inside another one and use its functions at runtime. The file may exist in my server or in another one.

How can I do this?

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Eslam Soliman
  • 1,276
  • 5
  • 16
  • 42
  • 1
    Possible duplicate: http://stackoverflow.com/questions/950087/include-javascript-file-inside-javascript-file – Petteri H Jul 26 '11 at 15:20

3 Answers3

8
var sc=document.createElement('script');
sc.src="script.js";
document.getElementsByTagName('head')[0].appendChild(sc);

Use something like this

Ruslan Polutsygan
  • 4,401
  • 2
  • 26
  • 37
3

the answer of this question you can find it in the following :Here

something like that visit the article to know more :

<script type="text/javascript">
// Function to allow one JavaScript file to be included by another.
// Copyright (C) 2006-08 www.cryer.co.uk
function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></scr' + 'ipt>'); 
}
</script>

and here are some links on stack overflow :

One Two

Community
  • 1
  • 1
Eslam Soliman
  • 1,276
  • 5
  • 16
  • 42
2

With JQuery power do

$.ajax({
  url: yourURL,
  dataType: "script",
  async: false
});
marc
  • 6,103
  • 1
  • 28
  • 33