Using notification to determine when Reflection is initialized

If you want to automate a task immediately after a Reflection terminal session loads, you must first ensure that the session is available and initialized before invoking other API methods. You can do this using some built-in features of the API in which the Reflection session sends notification to a JavaScript function on your web page when session initialization is complete, or by using an onLoad event handler in the web page's BODY tag. (See Timing issues when scripting Reflection for more information about script timing.)

Allowing Reflection to send a notification message to your JavaScript is much simpler and is more reliable than using an onLoad event handler, but there are some situations, such as when using VBScript, where the event handler method is needed. See Using an onLoad Handler to Determine When Reflection Is Initialized for an example of the polling method from an onLoad event handler.

There are two ways to use notification to determine Reflection's initialization state:

  • If you plan to use only those API methods available from the JavaScript API, add the parameter preloadJSAPI with a value of true to the Reflection applet tag. When this parameter is present, Reflection invokes a JavaScript function called jsapiInitialized when it has completed its initialization, passing the function a reference to the JavaScript API. Once your script receives this reference, you are guaranteed that the Reflection terminal session is initialized, and your JavaScript can proceed with any tasks it needs to perform. An example of this method is shown below.

  • If you plan to use both JavaScript API and ECL API functions from JavaScript, you can add an applet parameter to the Reflection session that specifies a built-in ECL module to run after the session is finished with initialization. When the API module runs, it calls a predefined JavaScript function on your web page, and as above, once your script receives this reference, you are guaranteed that the session is initialized. See Accessing the ECL API from JavaScript for more information about accessing ECL API methods from JavaScript.

Unless you need access to ECL API functions, the first method described above is recommended, since it requires a smaller download from the Reflection Management Server than when using the JSEventNotifier module.

The following JavaScript example shows how to use the jsapiInitialized function to receive notification when Reflection initialization is complete. Once notification is received, the Session Setup dialog box opens so the user can enter connection information and proceed with the session. In this example, both the Reflection terminal session applet and the JavaScript code are in the same HTML file.

To use this example, you must change the hostURL parameter in the <applet> tag below to a host appropriate for your network.

<html>
<head>
<title>Example of using Notification to Determine Reflection Initialization</title>
<script language="JavaScript">
<!--

   /*
      Variable to hold the API reference when it is retrieved.
   */
   var api = null;
   
   /*
      jsapiInitialized is the name of the function that Reflection calls when
      Reflection initialization is complete. It passes a reference to the
      JSAPI, which is stored in a variable for subsequent use by other script
      functions.
   */
   function jsapiInitialized( inapi )
   {
      api = inapi;
      if ( api == null )
      {
         alert( "Reflection API initialization failed." );
      }
      else
      {
         doSessionSetup();
      }
   }

    /*
       This function calls Reflection's showDialog method to
       open the Session Setup dialog box. This function is invoked
       only after notification is received that the terminal 
       session has been initialized.
    */
    function doSessionSetup()
    {
       api.showDialog( "sessionConfigure" );
    }
// -->
</script>
</head>
<!-- 
   The onLoad event handler is used to execute the JavaScript code
   after the page is loaded.
-->
<body>
<!--
   This is the tag that launches a Reflection IBM 3270 applet.
   To use this example, change the hostURL parameter to
   one appropriate for your network.
-->
<applet mayscript name="IBM3270"
        codebase="./ex/"
        code="com.wrq.rweb.Launcher.class"
        width="666" height="448"
        archive="Launcher.jar">
    <param name="hostURL" value="tn3270://accounts">
    <param name="autoconnect" value="false">
    <param name="launcher.sessions" value="IBM3270">
    <param name="preloadJSAPI" value="true">
</applet>

</body>
</html>