User's Guide
JavaScripting
OVMS v3 includes a powerful JavaScript engine. In addition to the standard, relatively fixed,
firmware flashed to the module, JavaScripting can be used to dynamically load script code to
run alongside the standard firmware. This javascript code can respond to system events, and
perform background monitoring and other such tasks.
The simplest way of running javascript is to place a piece of javascript code in the /store/scripts
directory, with the file extension ‘.js’. Then, the standard mechanism of running scripts can be
employed:
OVMS# . <script.js>
OVMS# script run <script.js>
Short javascript snippets can also be directly evaluated with:
OVMS# script eval <code>
Such javascript code can also be placed in the /store/events/<eventname> directories, to be
automatically executed when the specified event is triggered.
Persistent JavaScript
When a javascript script is executed, it is evaluated in the global javascript context. Care should
be taken that local variables may pollute that context, so it is in general recommended that all
JavaScript scripts are wrapped:
(function(){
… user code …
})();
It is also possible to deliberately load functions, and other code, into the global context
persistently, and have that code permanently available and running. When the JavaScript
engine initialises, it automatically runs a special startup script:
/store/script/ovmsmain.js
That script can in turn include other code. If you make a change to such persistent code, and
want to reload it, you can with:
OVMS# script reload
OVMS v3 User Guide Page 47 / 86