You can also just put placeholders or variables in the JS code and either replace them explicitly in the snippet, put the JS code in a chunk and get it with $modx->getChunk('chunkName', $fields) or just set the placeholders in the snippet. The JS code could also be in a file with placeholders. These are off the top of my head, so they may not be exactly right, but they should give you the idea.
Method 1 - For a simple case:
$var1 = 'someValue';
$var2 = 'someotherValue';
$jsCode = '
<script type="text/javascript">
function whatever() {
var1 = [[+var1]];
var2 = [[+var2]];
/* more code here */
}
</script>';
$jsCode = str_replace('[[+var1]]', $var1, $jsCode);
$jsCode = str_replace('[[+var2]]', $var2, $jsCode);
$modx->regClientStartupScript($jsCode);
method 2 - For more elaborate code:
jsChunk:
<script type="text/javascript">
function whatever() {
var1 = [[+var1]];
var2 = [[+var2]];
/* more code here */
};
</script>
Snippet code:
$fields = array(
'var1' = 'someValue',
'var2' = 'someOtherValue',
);
$jsCode = $modx->getChunk('jsChunk', $fields);
$modx->regClientStartupScript($jsCode);
method 3 - using a file
jsfile.js
function whatever() {
var1 = [[+var1]];
var2 = [[+var2]];
/* more code here */
};
Snippet code:
$fields = array(
'var1' = 'someValue',
'var2' = 'someOtherValue',
);
$modx->setPlaceholders($fields);
$modx->regClientStartupScript('jsfile.js');