8

Say my javascript scripts aren't embedded in html/xml... I want to write a small library of helper functions to use in my scripts. Obviously there has to be an "#include" or "require" keyword in javascript, but I can't find any. Anything I could with Google relies on the code being part of html/xml, which doesn't help in my case. What should I do?

4
  • What do you need to embed/require? Commented Apr 18, 2009 at 12:55
  • A .js file with several functions... I want to use these functions in other .js files. Commented Apr 18, 2009 at 12:56
  • What's wrong with function load(src){var s = document.createElement('script'); s.src = src; document.body.appendChild(s);} Commented Apr 18, 2009 at 13:21
  • @James he's not using HTML / XML. There is no DOM. document is not defined in this context. Commented Jan 23, 2014 at 0:31

4 Answers 4

17

I believe you mean to write some sort of dependency tracking framework for your javascript files and use a process called "Lazy-Loading" to load the required JS file only when it's needed.

Check out Using.js, it seems to do what you need.

Also you might want to check addModule from YUILoader. It allows loading non-YUI framework components on the fly.

Sign up to request clarification or add additional context in comments.

3 Comments

you must include Using.js first!
But wait -- don't these libraries just essentially add script tags to the head of the document? -- and therefore only work with JavaScript that's parsed in the context of a DOM (i.e. a web project)? The OP isn't doing web work, he's using JavaScript for something else completely different he said. There are no HTML files, etc.
This is a very old thread. If the OP was referring to a JS-only environment, like we have NodeJS nowadays, there are others solutions, like Node's own require().
3

There actually isn't a really #include or require in javascript. You're actually supposed to handle all the dependencies yourself. I've seen people do a hack where they do a document.write to include other javascript files.

Comments

0

in the case you care, here there is a version of include that uses the document object via it's DOM interface:

function include(aFilename) {
     var script = document.createElement('script');
     script.src = aFilename;
     script.type = 'text/javascript';
     document.getElementsByTagName('head')[0].appendChild(script)
}

the problem is that you must include this function in all your source file that needs includes... :P :P

2 Comments

aFilename not referenced from within the script. And how is this helpful?
include should have a call back.. because when we are including in the above manner we never know if all the required files have been included before thy are being used. Would prefer using the YUI.add() and YUI.use() functions
-2

to include a js file in html:

<script type="text/javascript" src="..."></script> 

it should be in the page head for correctness.

4 Comments

You can have the javascript write one of these in the head too.
Thanks but that's not what I meant at all. I have a .js file which I used outside of any html code. It's not for the web at all. Now I want to import another .js file into mine. How do I do that?
Ah, ok then the answer is no. You'd have to include the code directly in the js, or use a noddy html page to stitch the js files together ( & provide the UI).
Leave the JavaScript dependancies and behavior to the JavaScript programmers and let the HTML authors focus on the semantics. See separation of concerns

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.