Forums > Javascript

js object: Loading javascript files in subdirectory

December 14, 2009 | 10:34 pm

Is it possible to load javascript files from a subdirectory? My current project’s root path is getting pretty messy, and I want to organize my javascript files.

Something akin to: [js ./javascripts/script1.js]


December 14, 2009 | 10:58 pm

You can either add files to the search path using Options > File Preferences or use the [filepath] object to do this temporarily. If you want to include multiple javascript files in the current file then save the code below somewhere in the jsextensions folder.

lh

/*
* function to add other javascript files to your project
*
* first argument must be "this" to set scope
* second argument specifies the file to include
* relative paths must start wih a "/" separator
* this function uses eval() so be careful
*
* example
*
* include(this,"/local.js"); // same directory
* include(this,"/lib/js/functions/below.js"); // navigate lower
* include(this,"/../../above.js"); // navigate higher
* include(this,"/../../sub1/sub2/both.js"); // combine both
* include(this,"Macintosh HD:/Users/me/Desktop/other.js"); // absolute path
*/

function include(scope,dest) {
	var mem = "";
	var struct = /((/..)*)(.+$)/;
	var parent = /(.+)/[^.]+.w+$/;
	var levels = struct.exec(dest)[1].length/3;
	var name = struct.exec(dest)[3];
	var target;
	if (dest.charAt(0) != "/") {
		target = dest;
	} else if (!levels) {
		target = scope.patcher.filepath.match(parent)[1]+name;
	} else {
		target = scope.patcher.filepath;
		for (i=-1; i

February 14, 2010 | 9:54 am

hi,
when trying to use this, i get an error:

js: includefiles.js: Javascript TypeError: s has no properties, line 87

i’m a bit confused as there’s no line 87 in the file.
i have the above file saved in a .js file called includefiles.js in ‘Max5/Cycling ’74/jsextensions/’
basically i just have my .js file with a single line at the top:

include(this, "/../../../examples/javascript/math/dynexpr.js")

can anyone offer any assistance?
cheers,,

edit: sorry, it still works. so i guess it doesn’t matter?


February 14, 2010 | 12:08 pm

The way it reads the specified file into the current project is done line by line and the maximum line length is hard-coded to 800 characters so if you have code which exceeds this (if you’ve used a javascript compressor for the web which removes line breaks) then it is likely to break. It’s also important to make sure you’re looking in the right directory. Lastly the error might be in the file you are trying to include, so remember to check that too.

lh


February 14, 2010 | 2:20 pm

cheers. so i guess the error wasn’t really with the include() function, but rather with the file it was including, namely the dynexpr.js (which itself has only 54 lines of code). there was a variable s declared like:

expression = s.toString().replace(/"/g,""); // strip quotes if present

which caused it to complain because i didn’t include the default argument in my new js object (the script of which imported the old dynexpr.js). so thats all cool, cheers again for your help!


March 16, 2012 | 7:37 pm

[EDIT]

http://www.cycling74.com/forums/topic.php?id=38324

*bump*

trying to run this i get an error

js: includes.js: Javascript TypeError: scope.eval is not a function, line 49

refering to scope.eval(mem);

win7/6.0.4



Jan
March 16, 2012 | 10:03 pm

eval() is deprecated ans not supported buy Max6 anymore.



sxa
April 20, 2012 | 10:11 am

>>eval() is deprecated ans not supported buy Max6 anymore.

Could you elaborate? Is this in the documentation? I use my own simple eval()-based include mechanism and its working fine in 6.04/win7.


January 30, 2014 | 5:26 am

Eval is workingo n Max6:

<code>

– Pasted Max Patch, click to expand. –

</code>

I would be surprised if they really remove that functionality without adding something to replace it.


February 28, 2014 | 8:48 am

Hi it appears that the code in Luke’s original post cuts off mid function.

Is anyone else seeing that?


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