include() with JavaScript

Kinda like the PHP construct include.

<script type="text/javascript">

 function include(file) {
   if (document.createElement && document.getElementsByTagName) {
     var head = document.getElementsByTagName('head')[0];

     var script = document.createElement('script');
     script.setAttribute('type', 'text/javascript');
     script.setAttribute('src', file);

     head.appendChild(script);
   } else {
     alert('Your browser can\'t deal with the DOM standard. That means it\'s old. Go fix it!');
   }
 }

</script>

To include included.js with the above code just use:

<script type="text/javascript">

 include('included.js');

</script>

Home - More stuff