Client-side scripting

You can interact with the window displayed in a PowerBuilder window ActiveX by adding JavaScript or VBScript to the HTML page. You can:

NoteViewing ActiveX properties, events, and functions When the window ActiveX is registered on your machine, you can use the PowerBuilder Browser’s OLE tab to see the list of window ActiveX properties, events, and functions.

Coding event handlers

Your HTML page can contain JavaScript or VBScript event handlers for the PowerBuilder window ActiveX.

NoteCoding example assumptions The following code examples assume that the HTML page includes a Form, named buttonForm, which contains several Input elements: passedFlags, passedXPos, and passedYPos.

StepsTo code JavaScript event handlers for the PowerBuilder window ActiveX:

  1. Insert the PowerBuilder window ActiveX into the HTML page, specifying all necessary properties:

    <OBJECT NAME="PBRX1" WIDTH=225 HEIGHT=83
    
    CLASSID="CLSID:AAAA1304-A5A5-1000-8000-080009AC61A9">
    
    <PARAM NAME="_Version" VALUE="65536">
    
    <PARAM NAME="_ExtentX" VALUE="5962">
    
    <PARAM NAME="_ExtentY" VALUE="2164">
    
    <PARAM NAME="_StockProps" VALUE="0">
    
    <PARAM NAME="PBWindow" VALUE="w_helloworld">
    
    <PARAM NAME="LibList" VALUE="http://www.company.com/rknnt.pbd;">
    
    <PARAM NAME="PBApplication" VALUE="rknnt">
    
    <PARAM NAME="PBVersion" VALUE="105">
    
    </OBJECT>
    
  2. Within the heading of the HTML page, code a function to be called when the event occurs.

    The following sample function simply displays the arguments to the Clicked event.

    function wasClicked(flags, xpos, ypos) {
    
       document.buttonForm.passedFlags.value = flags;
    
       document.buttonForm.passedXPos.value = xpos;
    
       document.buttonForm.passedYPos.value = ypos;
    
    }
    
  3. Within the body of the HTML page, code an event handler that calls the function when the event occurs:

    <SCRIPT LANGUAGE="JavaScript" FOR="PBRX1" Event="Clicked(flags, xpos, ypos)">
    
    <!--
    
    wasClicked(flags, xpos, ypos);
    
    -->
    
    </SCRIPT>
    

    NoteCoding style Alternatively, you can omit the function call, placing all code within the event handler, as shown next.

StepsTo code VBScript event handlers for the PowerBuilder window ActiveX:

  1. Insert the PowerBuilder window ActiveX into the HTML page, specifying all necessary properties:

    <OBJECT NAME="PBRX1" WIDTH=225 HEIGHT=83
    
    CLASSID="CLSID:AAAA1304-A5A5-1000-8000-080009AC61A9">
    
    <PARAM NAME="_Version" VALUE="65536">
    
    <PARAM NAME="_ExtentX" VALUE="5962">
    
    <PARAM NAME="_ExtentY" VALUE="2164">
    
    <PARAM NAME="_StockProps" VALUE="0">
    
    <PARAM NAME="PBWindow" VALUE="w_helloworld">
    
    <PARAM NAME="LibList" VALUE="http://www.company.com/rknnt.pbd;">
    
    <PARAM NAME="PBApplication" VALUE="rknnt">
    
    <PARAM NAME="PBVersion" VALUE="105">
    
    </OBJECT>
    
  2. Within the body of the HTML page, code an event handler that processes the event.

    This sample function simply displays the arguments to the Clicked event:

    <SCRIPT LANGUAGE="VBScript">
    
    <!--
    
    Sub PBRX1_Clicked(flags, xpos, ypos)
    
       document.buttonForm.passedFlags.value = flags
    
       document.buttonForm.passedXPos.value = xpos
    
       document.buttonForm.passedYPos.value = ypos
    
    end sub
    
    -->
    
    </SCRIPT>
    

Calling PowerScript functions

The PowerBuilder window ActiveX allows you to call certain PowerScript functions on the window displayed in the Active control:

For more information on these functions, see the PowerScript Reference.

As with all ActiveX controls, the PowerBuilder window ActiveX provides an AboutBox function, which you can call to see information about the control.

NoteCoding example assumptions The following coding examples assume that you have written scripts to be invoked by the PowerBuilder window’s Timer event.

StepsTo use JavaScript to call a PowerScript function:

  1. Code a function to call the PowerScript function.

    This example calls the PowerScript Timer function:

    <SCRIPT LANGUAGE="JavaScript">
    
    <!--
    
    var cumSeconds = 0;
    
    function setPBTimer( f ) {
    
    var li_return
    
    var li_interval
    
    li_interval = parseInt(f.timerInterval.value);
    
    li_return = PBRX1.Timer(li_interval);
    
    if (li_return != 1) {
    
       alert("Set Timer failed");
    
    }
    
    }
    
    //-->
    
    /SCRIPT
    
  2. Code a function, anchor, or button that calls the function.

    This example uses a button on a form to call the function defined above, which resets the timer interval:

    <FORM NAME="clockForm">
    
    <P>Timer Interval:
    
    <INPUT TYPE=Text NAME="timerInterval" Size="5">
    
    <P><INPUT TYPE=BUTTON VALUE="Set Timer"
    
    ONCLICK="setPBTimer(this.form)">
    
    </FORM>
    

StepsTo use VBScript to call a PowerScript function:

  1. Code a function to call the PowerScript function. This example calls the PowerScript Timer function:

    <SCRIPT LANGUAGE="VBScript">
    
    <!--
    
    dim cumSeconds
    
    cumSeconds = 0
    
    Sub pbSetTime()
    
      dim li_return
    
      dim li_interval
    
      li_interval = clockForm.timerInterval.value
    
      li_return = pbrx1.Timer(li_interval)
    
      if li_return  1 THEN
    
        msgBox "Set Timer failed"
    
      end if
    
      end sub
    
    //-->
    
    </SCRIPT>
    
  2. Code a function, anchor, or button that calls the function.

    This example uses a button on a form to call the function defined above, which resets the timer interval:

    <FORM NAME="clockForm">
    
    <P>Timer Interval: 
    
    <INPUT TYPE=Text NAME="timerInterval" Size="5">
    
    <HR>
    
    <P>Mirror of PB Time: 
    
    <INPUT TYPE=Text NAME="pbTime" Size="8">
    
    <HR>
    
    <P>INPUT TYPE=BUTTON VALUE="Set Timer" onClick="call pbSetTime()">
    
    </FORM>
    

Calling user-defined functions

The PowerBuilder window ActiveX provides the InvokePBFunction function, which you can use to call a user-defined window function.

NoteVBScript and JavaScript differ If your user-defined functions contain arguments and you are using JavaScript, you must use SetArgElement to specify the arguments; you cannot specify the arguments explicitly in the InvokePBFunction function.

StepsTo code JavaScript that invokes a user-defined function:

  1. Define window functions as needed.

    The following example assumes that in the PowerBuilder window, you have defined the function of_arg that takes a string as a parameter.

  2. Code a JavaScript function that calls the InvokePBFunction function, specifying the user-defined function to invoke.

    This example initializes arguments and calls the of_args window function:

    function invokeFunc(f) {
    
      var retcd;
    
      var rc;
    
      var numargs;
    
      var theFunc;
    
      var theArg;
    
      retcd = 0;
    
      numargs = 1;
    
      theArg = f.textToPB.value;
    
      PBRX1.SetArgElement(1, theArg);
    
      theFunc = "of_args";
    
      retcd = PBRX1.InvokePBFunction(theFunc, numargs);
    
      rc = parseInt(PBRX1.GetLastReturn());
    
      if (rc != 1) {
    
        alert("Error. Empty string.");
    
      }
    
      PBRX1.ResetArgElements();
    
    }
    
  3. Code a function, anchor, or form button that invokes your JavaScript function. For example:

    <FORM>
    
    <P>Copy this text to PowerBuilder:
    
    <INPUT TYPE=Text NAME="textToPB" SIZE="20">
    
    <P><INPUT TYPE=BUTTON VALUE="Invoke Func"
    
    ONCLICK="invokeFunc(this.Form)">
    
    </FORM>
    

NoteDefining arguments in JavaScript When coding in JavaScript, define function and event arguments by calling the SetArgElement function.

StepsTo code VBScript that invokes a user-defined function:

  1. Define window functions as needed.

    The following example assumes that in the PowerBuilder window you have defined the function of_arg that takes a string as a parameter.

  2. Code a VBScript function that calls the InvokePBFunction function, specifying the user-defined function to invoke.

    This example initializes arguments and calls the of_args window function:

    Sub invokeFunction()
    
      Dim retcd
    
      Dim myForm
    
      Dim args(1)
    
      Dim rc
    
      Dim numargs
    
      Dim theFunc
    
      Dim rcfromfunc
    
      retcd = 0
    
      numargs = 1
    
      rc = 0
    
      theFunc = "of_args"
    
      Set myForm = Document.buttonForm
    
      args(0) = buttonForm.textToPB.value
    
      retcd = PBRX1.InvokePBFunction(theFunc, 
        numargs, args)
    
      rc = PBRX1.GetLastReturn()
    
      if rc  1 then
    
        msgbox "Error. Empty string."
    
      end if
    
      PBRX1.ResetArgElements()
    
    end sub
    
  3. Code a function, anchor, or form button whose click invokes your VBScript function. For example:

    <FORM NAME="buttonForm">
    
    <P><INPUT TYPE=Text NAME="textToPB" SIZE="20">
    
    <P><INPUT TYPE=BUTTON VALUE="Invoke Function" ONCLICK="call invokeFunction()">
    
    </FORM>
    

Calling user events

The PowerBuilder window ActiveX provides the TriggerPBEvent function, which you can use to call a user event on the window.

StepsTo code JavaScript that triggers a user event:

  1. Define user events on the window as needed.

    The following example assumes that in the PowerBuilder window you have defined the user event ue_args that takes a string as an argument.

  2. Code a function to call the user event. This example initializes arguments and calls the ue_args window event:

    function triggerEvent(f) {
    
      var retcd;
    
      var rc;
    
      var numargs;
    
      var theEvent;
    
      var theArg;
    
      retcd = 0;
    
      numargs = 1;
    
      theArg = f.textToPB.value;
    
      PBRX1.SetArgElement(1, theArg);
    
      theEvent = "ue_args";
    
      retcd = PBRX1.TriggerPBEvent(theEvent, numargs);
    
      rc = parseInt(PBRX1.GetLastReturn());
    
      if (rc != 1) {
    
        alert("Error. Empty string.");
    
      }
    
      PBRX1.ResetArgElements();
    
    }
    
  3. Code a function, anchor, or form button that calls the TriggerPBEvent function. For example:

    <FORM>
    
    <P><INPUT TYPE=Text NAME="textToPB" SIZE="20">
    
    <P><INPUT TYPE=BUTTON VALUE="Trigger Event"
    
    ONCLICK="triggerEvent(this.Form)">
    
    </FORM>
    

StepsTo code VBScript that triggers a user event:

  1. Define user events on the window, as needed.

    The following example assumes that in the PowerBuilder window you have defined the user event ue_args that takes a string as an argument.

  2. Code a function to call the user event. This example initializes arguments and calls the ue_args window function:

    Sub TrigEvent( )
    
      Dim retcd
    
      Dim myForm
    
      Dim args(1)
    
      Dim rc
    
      Dim numargs
    
      Dim theEvent
    
      retcd = 0
    
      numargs = 1
    
      rc = 0
    
      theEvent = "ue_args"
    
      Set myForm = Document.buttonForm
    
      args(0) = buttonForm.textToPB.value
    
      retcd = PBRX1.TriggerPBEvent(theEvent,
    
     			numargs, args)
    
      rc = PBRX1.GetLastReturn()
    
      if rc  1 then
    
        msgbox "Error. Empty string."
    
      end if
    
      PBRX1.ResetArgElements()
    
    end sub
    
  3. Code a function, anchor, or form button whose click invokes your VBScript function. For example:

    <FORM NAME="buttonForm">
    
    <P><INPUT TYPE=Text NAME="textToPB" SIZE="20">
    
    <P><INPUT TYPE=BUTTON VALUE="Trigger Event" ONCLICK="call TrigEvent()">
    
    </FORM>