Jump to content

Javascript includes?


phpknight

Recommended Posts

Hi,

 

I am making a few .js scripts outside of any HTML environment.  How does javascript let you include other javascript files in a .js file?  I want to build up a function or class library, and I cannot figure out how to tell one .js file to look for functions in another when you are not using html as well.  It seems simple enough, so hopefully it is possible.

Link to comment
Share on other sites

AFAIK, so long as you link to your library code before linking to your specific client code, things should run fine.  In other words:

<script type="text/javascript" src="library.js"></script>
<script type="text/javascript" src="specific-client-code.js"></script>

Link to comment
Share on other sites

Sounds like a job for DOM. I had a project once where I needed to swap out "included" js code on-the-fly and did something like this:

 

<script type="text/javascript" src="library.js" id="JS_include"></script>

function changeJS(includeFile) {

    document.getElementById(JS_include).src = includeFile;

}

 

You should be able to "add" javascript includes through the DOM and set their src attribute, but my DOM skills are not the best.

 

EDIT: I was correct. Here is a page with the instructions for doing just that: http://www.chapter31.com/2006/12/07/including-js-files-from-within-js-files/

 

Here is the JS code from that page

//this function includes all necessary js files for the application   
function include(file)   
{   
  
  var script  = document.createElement('script');   
  script.src  = file;   
  script.type = 'text/javascript';   
  script.defer = true;   
  
  document.getElementsByTagName('head').item(0).appendChild(script);   
  
}   
  
/* include any js files here */   
include('js/myFile1.js');   
include('js/myFile2.js');   

Link to comment
Share on other sites

I think that include syntax might work, but can I write that inside a .js file?  Basically, I want to include my code library as a header in my .js file just like C++ or PHP, but it cannot be HTML files because I am doing this in a different environment.  How is it that this would not be possible? 

Link to comment
Share on other sites

Not to be rude, but it would take you about 2 minutes to verify that for yourself. As I stated in my first post you just need to use the DOM to dynamically "add" a script tag. That's what the author of that code states it does. Since it is entirely JS you can add it in any javascript you want: in the head, inline or in an external file.

Link to comment
Share on other sites

That didn't work for me, but like you, I am not sure I know enough about DOM to fix it.  I get errors both with the solution you linked ('document undefined') and yours ('variable or value expected').  The problem is that when the script begins there is no document node.  The script opens the documents, too.

 

So, I'll just keep plugging along here and let you know if I run across something.  Thanks for the help.

Link to comment
Share on other sites

It works for me. Here are the three test files I created:

 

HTML File:

<html>
  <head>
    <script type="text/javascript" src="library.js"></script>
  </head>

  <body>
    Testing dynamically loaded JS
  </body>
</html>

 

Librabry JS File

function include(file)   
{   
  
  var script  = document.createElement('script');   
  script.src  = file;   
  script.type = 'text/javascript';   
  script.defer = true;   
  
  document.getElementsByTagName('head').item(0).appendChild(script);   
  
}   

include('include1.js');

 

Dynamically included JS file

alert('Loaded JS 1');

Link to comment
Share on other sites

I see. So what are you using these for? If you are creating scripts to do something on your PC there are much better scripting languages for that purpose than JS. Even so, you could still start the initial js file using an HTML page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.