41 lines
1.5 KiB
Markdown
41 lines
1.5 KiB
Markdown
# Including scripts within Oxygen [JS Operations](https://www.oxygenxml.com/doc/versions/23.1/ug-editor/topics/dg-default-author-operations.html#dg-default-author-operations__jsoperation)
|
|
|
|
Another limitation of the JS operation is that a single "globals.js" file is loaded before the execution of javascript operation.
|
|
|
|
This rambling proposes a way to include other scripts at runtime.
|
|
|
|
## Installation
|
|
|
|
To do so, add this function to your commons.js file:
|
|
|
|
```javascript
|
|
include = function (filepath, isAbsolute) {
|
|
if (isAbsolute === undefined || isAbsolute == false) {
|
|
filepath = Packages.ro.sync.ecss.extensions.commons.operations.CommonsOperationsUtil.expandAndResolvePath(authorAccess, filepath);
|
|
}
|
|
var text = new java.lang.String(java.nio.file.Files.readAllBytes(java.nio.file.Paths. get (java.net.URI(filepath))));
|
|
text = String(text);
|
|
eval(text);
|
|
}
|
|
```
|
|
|
|
And use the include() function it in the script parameter of the JS operation:
|
|
```javascript
|
|
doOperation = function () {
|
|
include('hello.js');
|
|
hello();
|
|
}
|
|
```
|
|
|
|
## Limitations
|
|
The function needs an authorAccess variable in order to resolve relative URIs and this variable is only available in the scope of the doOperation function. This means that you can't use it
|
|
outside of this function call, like, for instance, in the global scope of the common.js script.
|
|
## See also
|
|
* [debugger](../js-debugger)
|
|
* [debugger + inclusion](../js-debugger-include)
|
|
# Framework
|
|
|
|
This directory includes the Oxygen sample framework on which ou can test this technique but it should work on any framework.
|
|
|
|
|