Using an onLoad handler 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 by using the JavaScript onLoad event handler in the web page's BODY tag, or by waiting for notification when the session is done with initialization. (See Timing issues when scripting Reflection for more information about script timing.)

The following JavaScript example shows how to use an onLoad handler to determine if a Reflection session is initialized. It also uses an error handler, so if Reflection is not initialized enough to access the getInitialized method, the function that performs the initialization check can retry the check after a short delay. Once Reflection is initialized, 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.

See Using notification to determine when Reflection is initialized for an example of the notification method.

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 an onLoad Handler to Determine Reflection Initialization</title>
<script language="JavaScript">
<!--
    /*
       Set an error handler for the window.
    */
    window.onerror = errHandler;

    /*
       Create a few variables to hold the target applet, the API, the 
       initialization state, and a couple of counters.
    */
    var appletTarget = null;
    
    var inited = 0;
    var count = 0;
    var errcount = 0;
    
    /*
       The error handler is invoked if an attempt to get the applet
       initialization state fails (because Reflection is not yet available),
       or if the api is null and the getInitialized() method is invoked.
       The handler will retry the attempt to get the session's 
       initialization state up to 10 more times, waiting 2 seconds between
       attempts. By returning a value of true from the error handler, no
       JavaScript error alert appears.
    */
    function errHandler( msg, url, line )
    {
        // To ensure that the error handler was invoked because the
        // initialization state is not available and not because some
        // other code failed, first check the "inited" variable before
        // setting the timeout.
        if ( inited <= 0 ) {
            if ( errcount < 10 ) {
                errcount++;
                setTimeout( "tryInit()", 2000 );
            }
            else {
                alert( "Unable to initialize Reflection." );
            }
        }
        return true;
    }

    /*
       This function first gets a reference to the Reflection applet on the
       page and stores it in "appletTarget". Then the function tries to get
       the Reflection API, and then the initialization state, using the getAPI()
       and getInitialized() methods. It then sets the "inited" variable 
       to the return value of getInitialized(). If the "inited" variable 
       is true, the function that opens the Session Setup dialog box is called. 
       If the call to Reflection's getInitialized method fails (because the session 
       or API isn't available yet), the error handler is invoked to repeat the 
       attempt. If the session is available but not yet initialized, a timeout 
       is set to retry this same function every 2 seconds, for up to 10 
       attempts or until "inited" is true.
    */
    function tryInit()
    {
        appletTarget = document.IBM3270;
        api = appletTarget.getAPI("JSAPI","IBM3270");
        inited = api.getInitialized();
        if ( inited > 0 )
        {
            doSessionSetup();
        }
        else if ( inited < 0 )
        {
            alert( "Reflection initialization failed." );
        }
        else if ( count < 10 )
        {
            count++;
            setTimeout( "tryInit()", 2000 );
        }
        else
        {
            alert( "Unable to initialize Reflection." );
        }
    }

    /*
       This function calls Reflection's showDialog method to
       open the Session Setup dialog box. This function is invoked
       only after 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 onLoad="tryInit()">
<!--
   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>