By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,761 Members | 1,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,761 IT Pros & Developers. It's quick & easy.

Structuring Javascript code

P: n/a
I hope you don't mind trivial questions. I only ask them after failing to
find answers in my book and on the web...

Can I split Javascript code into separate js files which reference each
other (hierarchically)? For instance, can I put core functions in a library
file and refer to them from other script files (like: $´nclude "core.js").

TIA
Tom
Feb 20 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Wed, 20 Feb 2008 17:40:11 +0100, Tom de Neef wrote:
Can I split Javascript code into separate js files which reference each
other (hierarchically)? For instance, can I put core functions in a
library file and refer to them from other script files (like: $nclude
"core.js").
You can separate javascript into separate files that are read in with the
<scripttag. You need one tag per javascript file to include.

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

(There are some tricks make one javascript file to include the others, but
that is often regarded as 'dirty' at best and unreliable at worst.)

Feb 20 '08 #2

P: n/a
On Feb 20, 8:40 am, "Tom de Neef" <tden...@qolor.nlwrote:
I hope you don't mind trivial questions. I only ask them after failing to
find answers in my book and on the web...

Can I split Javascript code into separate js files which reference each
other (hierarchically)? For instance, can I put core functions in a library
file and refer to them from other script files (like: $´nclude "core.js").
There are quite a few "module loader" systems out there that allow
building dependencies like this. I don't use such a system. I just
manually list them in the page like this

<script type="text/javascript src="core.js"></script>
<script type="text/javascript src="app.js"></script>

I think the module loaders encourage making many HTTP requests to the
server and that is a bad idea. It is better to have a build system
that aggregates the various JavaScript development files into
production files with the whitespace removed etc.

Peter
Feb 20 '08 #3

P: n/a
"Peter Michaux" <pe**********@gmail.comschreef in bericht
news:0b**********************************@o10g2000 hsf.googlegroups.com...
On Feb 20, 8:40 am, "Tom de Neef" <tden...@qolor.nlwrote:
I hope you don't mind trivial questions. I only ask them after failing to
find answers in my book and on the web...

Can I split Javascript code into separate js files which reference each
other (hierarchically)? For instance, can I put core functions in a
library
file and refer to them from other script files (like: $´nclude "core.js").

<script type="text/javascript src="core.js"></script>
<script type="text/javascript src="app.js"></script>

So, the HTML will bind it all together. That is fine: a function defined in
"core.js" can be referenced in "app.js" and within the HTML it will work
together.

That does not solve the problem of how to test "app.js" without the HTML, if
it calls a function declared in "core.js". Or do you never test the script
standalone? In my editor (Antechinus) standalone testing is an option.
Tom
Feb 20 '08 #4

P: n/a
On Feb 20, 10:43 am, "Tom de Neef" <tden...@qolor.nlwrote:
"Peter Michaux" <petermich...@gmail.comschreef in berichtnews:0b**********************************@o 10g2000hsf.googlegroups.com...
On Feb 20, 8:40 am, "Tom de Neef" <tden...@qolor.nlwrote:
I hope you don't mind trivial questions. I only ask them after failing to
find answers in my book and on the web...
Can I split Javascript code into separate js files which reference each
other (hierarchically)? For instance, can I put core functions in a
library
file and refer to them from other script files (like: $´nclude "core.js").

<script type="text/javascript src="core.js"></script>
<script type="text/javascript src="app.js"></script>

So, the HTML will bind it all together. That is fine: a function defined in
"core.js" can be referenced in "app.js" and within the HTML it will work
together.
You are correct. All the scripts are evaluated in the same global
namespace.
That does not solve the problem of how to test "app.js" without the HTML, if
it calls a function declared in "core.js". Or do you never test the script
standalone? In my editor (Antechinus) standalone testing is an option.
I suppose the editor should be able to be told which scripts to "load"
when testing a particular script.

Peter
Feb 20 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.