The JavaScript code executed by Creo.JS must be included in the <script> tag, which has the type set as text/creojs. The src attribute is supported for scripts included in the web page. The scripts must be located relative to the HTML page location.
Absolute scripts references, for example, src=http://some-server/foobar.creojs are not supported.
After a web page loads, Creo.JS framework scans the DOM of the web page. It loads all the Creo.JS scripts in the connected Creo session. The scripts are executed by Creo.JS engine.
Creo.JS scripts are loaded in Creo and executed in the order of their appearance on the web page. The Creo.JS scripts are loaded and executed asynchronously when the web page loads.
When the web page fully loads, Creo.JS scripts may not have been fully loaded, even though all page elements onload event listeners are executed. If you want the web page to wait for Creo.JS initialization and termination events, set the event listeners CreoJS.$ADD_ON_LOAD and CreoJS.$ADD_ON_UNLOAD respectively.
The
CreoJS.$ADD_ON_LOAD adds an event handler that executes the initialization script only after all the
Creo.JS scripts are loaded. To ensure that the initialization code which interacts with
Creo.JS code is executed after all page scripts are initialized, set
Creo.JS onload event handler inside the
onload event handler of the page in the
<body> tag. See the following sample code:
<html>
<head>
<script src="creojs.js"></script>
<script type="text/creojs" src="browser.creojs"></script>
<script type="text/creojs">
function getActiveModelName () {
return pfcGetCurrentSession ().GetActiveModel ().FileName;
}
</script>
</head>
<body onload="CreoJS.$ADD_ON_LOAD (initialize)">
<b>Active model:</b> <span id="MODELNAME"></span>
<script>
function initialize () {
CreoJS.getActiveModelName ()
.then (function (name) {
MODELNAME.innerHTML = name;
})
}
</script>
</body>
</html>
Use the CreoJS.$ADD_ON_UNLOAD event handler to execute the deinitialization code when the browser unloads the page. These handlers execute the script before
Creo.JS destroys context associated with the page being unloaded. You can also release the Creo resources allocated by the page.
Each web page has its own isolated context inside the Creo.JS engine. The context is created when the web page is loading and destroyed when the web page is unloading. The pages can communicate
with each other using the Creo.JS session storage, which maintains a global context of Creo.JS engine. This is similar to the browser property Window.sessionStorage.
Embedded Scripts
It is recommended to use the id attribute for embedded Creo.JS scripts. The id are returned in errors which further helps to identify location of the problem.
The following example shows an embedded script.
<script type="text/creojs" id=”example1.creojs”>
function getActiveModelName () {
let session = pfcGetCurrentSession ();
let model = session.GetActiveModel ();
return model ? model.FileName : null
}
</script>
External Scripts
When you use external Creo.JS scripts it is recommended to use .creojs extension. Do not use .js extensions for external scripts. The .js scripts may not load properly in the web page due to the file type association of the system.
If scripts do not load in Creo with .creojs extension , check your system for .creojs file type association. If there is a default program assigned to handle .creojs files, remove it to resolve this problem.
This following example shows an external script.
<script type=”text/creojs” src=”test/example3.creojs”></script>