phpknight Posted May 11, 2008 Share Posted May 11, 2008 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. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 12, 2008 Share Posted May 12, 2008 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> Quote Link to comment Share on other sites More sharing options...
phpknight Posted May 12, 2008 Author Share Posted May 12, 2008 Okay, but I can write that in another .js file and not an HTML file? Will that work in any platform? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 12, 2008 Share Posted May 12, 2008 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'); Quote Link to comment Share on other sites More sharing options...
phpknight Posted May 13, 2008 Author Share Posted May 13, 2008 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 13, 2008 Share Posted May 13, 2008 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. Quote Link to comment Share on other sites More sharing options...
phpknight Posted May 13, 2008 Author Share Posted May 13, 2008 Okay, I will try that and get back to you. I'm working on so many things at once it is difficult for me to keep up. Quote Link to comment Share on other sites More sharing options...
phpknight Posted May 13, 2008 Author Share Posted May 13, 2008 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 13, 2008 Share Posted May 13, 2008 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'); Quote Link to comment Share on other sites More sharing options...
phpknight Posted May 13, 2008 Author Share Posted May 13, 2008 Hi, Yeah, that is problem. I am not coding this in an HTML environment, so I cannot implement that part correctly. When my script runs, there are no open documents. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 13, 2008 Share Posted May 13, 2008 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. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted May 15, 2008 Share Posted May 15, 2008 if its gonna be a desktop application why not use PHP-GTK since you probably do have experience with php Quote Link to comment Share on other sites More sharing options...
phpknight Posted May 15, 2008 Author Share Posted May 15, 2008 Actually, it is an Adobe product, and they limit what languages you can write script in. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted May 15, 2008 Share Posted May 15, 2008 ah i see i used javascript with photoshop a couple of times. i guess the only way is just paste the js sourse inside the script will have to do Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.