B4J Question WebSocket - Run Sub from HTML Function

Discussion in 'B4J Questions' started by aaronk, Jul 16, 2015.

  1. aaronk

    aaronk Well-Known Member Licensed User

    Hi,

    I am using WebSockets in my B4J app and I am trying to work out how to trigger a sub in my B4J project from a HTML page function.

    Looking though the tutorial you need to put a sub in the B4J app like:

    Code:
    Sub btnCalc_Click (Params As Map)
         Result.SetText(
    "Hello World")
    End Sub
    And using a button in the HTML page:
    HTML:
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Web App Hello World!</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
      <link rel="stylesheet" type="text/css" href="index.css" />
      <script src="/b4j_ws.js"></script>
    </head>
    <body>
      <h1>Web App Hello World!</h1>
      <p id="plog"></p>
      <div id="maindiv">
      First Number: <input id="text1" type="text"></input><br/>
      Second Number: <input id="text2" type="text"></input><br/>
      <button id="btncalc">Calculate</button><br/>
      <p id="result"></p>
      </div>
       <script>
      //connect to the web socket when the page is ready.
      $( document ).ready(function() {
      b4j_connect("/ws");
      });
       </script>
    </body>
    Everything is working fine so far..

    But rather than using a button to trigger the code in the B4J app (btnCalc_Click) is there a way to run the sub in the B4J from a function in the HTML page ?

    HTML:
    <button onClick="RunFunction();">Click Me</button>
    
    function RunFunction()
    {
        // What code can I use here to run the Sub in my B4J app?
    }
     
  2. billzhan

    billzhan Active Member Licensed User

    try

    Code:
    function RunFunction()
    {
        b4j_raiseEvent(
    "btnCalc_Click", {}) ;
    }
     
  3. aaronk

    aaronk Well-Known Member Licensed User

    That did the job.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice