Can i break up a js into multiple files?

Mar 18, 2010 at 2:49am

Can i break up a js into multiple files?

My js is getting unmanageably long…

Mar 18, 2010 at 6:38pm

Yes, but you might not want to.

There are 2 ways to do this that I’m aware of:

1. Put your code (or some of it) in your jsextensions folder.

2. Use File + eval:

/* seriously, don't use this unless you really know what's in the file. */
jsInclude.local = 1;
function jsInclude(filename) {
    var f = new File(filename);

readWholeFile.local = 1;
function readWholeFile(f) {
    var BLOCK_SIZE = 1024;
    if(f.isopen) {
	var s = "";
	var l;
	while(l = f.readstring(1024)) {
	    s += l;
	return s;
Mar 19, 2010 at 4:08pm

yeah, both of those seem like last resorts.

ok. other than ‘too much scrolling,’ is there a drawback to a long script? i thought there was a length limit, but i seem to have been mistaken.

Thank you

Mar 19, 2010 at 5:22pm

Hello maxer,

i have a 3000 lines javscript who works fine ; perhaps instantiation is a bit long, but i’m not sure, as the max patch is huge too :-)

Mar 20, 2010 at 2:04am

ok, well, it looks like that is the path i will take.

Mar 20, 2010 at 6:46am

Hello maxer,

with a good IDE (Eclipse, Xcode …) and autowatch = 1 ; that’s not so painful …

Mar 20, 2010 at 1:23pm

i am fond of xCode. pragma marks are my friend. but editing js in it seems sclunky compared to the smooth loving it gives me with obj C and the likes

Thank you though!

Mar 21, 2010 at 4:31am

I should mention, there is a third way, which is to use standard #include directives and then run your code through the C preprocessor. In other words, write your code in separate files, and use #include "myothercode.js" where you want to refer to another file. Then run the central file through the preprocessor to produce the file you actually point max at.

Mar 21, 2010 at 11:11am

that makes wonderful sense and i think i will do that.


You must be logged in to reply to this topic.