Communication from the Web Dynpro Back End to the Client

In this example, the user should be able to use a button to trigger a JavaScript call to the backend . When the user presses this button, a popup should appear with the text "Hello World!".

For this there must be a JavaScript object myJSVariable in the browser that has the function greet with a string parameter.

First create a JavaScript file with the following code:

var myJSVariable = {
          greet: function (message) {          
alert(message);          
}          
};
            

You can either include this JavaScript file using the aggregation SCRIPTS in an HTMLIsland or in an HTMLContainer.

To formulate the call IF_WD_HTML_SCRIPT_CALL using API myJSVariable.greet('Hello World'), you can use the following code in the onAction event handling method of the Button:

          
method ONACTIONGREET .
          
wd_this->m_html_island->add_script_call(
          
cl_wd_html_script_call=>new_call(
          
)->variable( `myJSVariable`
          
)->function( `greet`
          
)->add_string( `Hello World`!` ).
          
).
          
endmethod.