How to enable # javascript?

Say my javascript scripts are not embedded in html / xml ... I want to write a small library of helper functions for use in my scripts. Obviously, javascript should have the keyword "#include" or "require", but I cannot find it. Everything I could with Google relies on code that is part of html / xml, which in my case does not help. What should I do?

+6
javascript
source share
4 answers

I believe that you want to write some kind of dependency tracking structure for your javascript files and use the "Lazy-Loading" process to download the necessary JS file only when necessary.

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

You can also check addModule on YUILoader . It allows you to load non-YUI infrastructure components on the fly.

+17
source share

This is actually not #include or require in javascript. You really have to handle all the dependencies yourself. I saw people doing hack where they do document.write to include other javascript files.

+2
source share

in the event that you do not care, there is an include version that uses the document object via the 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 have to include this function in the whole source file that needs ...: P: P

-one
source share

include js file in html:

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

it should be in the page title for correctness.

-2
source share

All Articles