Forums > Javascript

Can i break up a js into multiple files?

March 18, 2010 | 2:49 am

My js is getting unmanageably long…


March 18, 2010 | 6:38 pm

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);
    eval(readWholeFile(f));
}

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;
    }
}

March 19, 2010 | 4:08 pm

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


March 20, 2010 | 2:04 am

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


March 20, 2010 | 1:23 pm

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!


March 21, 2010 | 4:31 am

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.


March 21, 2010 | 11:11 am

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


Viewing 7 posts - 1 through 7 (of 7 total)