Including a JavaScript File

We can include a javascript file dynamically like other languages in your javascript code. Here is the source code to include a javascript file dynamically.

  1.  
  2. <script language="JavaScript">
  3.  
  4.  function include_js(file)
  5.  
  6.  {
  7.  
  8.      var include_file = document.createElement(’script’);
  9.  
  10.      include_file.type = ‘text/javascript’;
  11.  
  12.         include_file.src = file;
  13.  
  14.         document.getElementsByTagName(‘head’)[0].appendChild(include_file);
  15.  
  16.  }
  17.  
  18. /* include your file */
  19.  
  20.  include_js(‘myscript.js’);
  21.  
  22. </script>

Here I have used the DOM to create the script object.

delicious | digg | reddit | facebook | technorati | stumbleupon | chatintamil
Report This Post

No comments yet.

Write a comment: