Is it possible to do something like the following: index.html: <head> <script src="a.js"></script> </head> Code (markup): a.js <script src="b.js"></script> functionThatRequiresB(); Code (markup): b.js code that lets the prior function work Code (markup):
i'm not sure if that's possible.. but i'm pretty sure this will do the trick <head> <script src="a.js"></script> <script src="b.js"></script> </head>
Sorry for the necro. I thought I had email notification turned on, and assumed nobody had responded to the thread, so just saw this. I want scripts to be able to access libraries themselves without having to include all of it's prerequisite files externally, sort of like using the "#include" directive inside a C header file to include another header file. You free the end users and maintainers from needing to fully understand your toolset in order to use it. Basically I want to make my libraries more user friendly by using: <script src="mylibrary.js"></script> instead of <script src="prereq1.js"></script> <script src="prereq2.js"></script> <script src="mylibrary.js"></script>
It seems there is no simple solution for that. At best you could try to dynamically create DOM scripts elements and include needed files. Look at this thread, it has few nice solutions: http://www.thescripts.com/forum/thread149165.html
Are you asking for import a javascript from other javascript (inside or outside your html) ? If yes, you can use: document.write("<script type='text/javascript' src='test.js'></scr"+"ipt>"); Code (markup): Note that you should split the closing script tag into two parts. Another option is using the DOM model: // Create the Script Object var script = document.createElement('script'); script.src = 'server.js'; script.type = 'text/javascript'; script.defer = true; script.id = 'scriptID'; // This will help us in referencing the object later for removal // Insert the created object to the html head element var head = document.getElementsByTagName('head').item(0); head.appendChild(script); Code (markup): And to remove that object: var head = document.getElementsByTagName('head').item(0); var old = document.getElementById('scriptID'); if (old) head.removeChild(old); Code (markup):
The first string "</javascript>" found will mark the end of javascript code. So that's to avoid that thing, because your javascript code does not end there.