Can i break up a js into multiple files?

Griotspeak's icon

My js is getting unmanageably long...

Brian Gruber's icon

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

Griotspeak's icon

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

Griotspeak's icon

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

Griotspeak's icon

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!

Brian Gruber's icon

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.

Griotspeak's icon

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