Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, February 21, 2008

How to use JavaScript in SAP ABAP?

You can code JavaScript directly in your ABAP code.

If you want to do some JavaScript tests in ABAP there is a good program to use : DEMO_JAVA_SCRIPT_MINI_EDITOR. When you run this program, you can code your Java in an ABAP editor, compile and execute it to test the result.

Nowe, here is a small example on how to insert JavaScript in ABAP:

*&---------------------------------------------------------------------*
*& Report ZDANY_JAVASCRIPT_TEST
*&---------------------------------------------------------------------*
REPORT ZDANY_JAVASCRIPT_TEST.
     
data: l_JS_PROCESSOR type ref to CL_JAVA_SCRIPT,
      l_RETURN_VALUE type STRING,
      l_SOURCE       type STRING.
     
* Creation of a new javaScript
l_JS_PROCESSOR = CL_JAVA_SCRIPT=>CREATE( ).
     
* this is the Javascript code
concatenate
   'var l_source = "Hello World,"; '
   'l_source += " I''m"; '
   'l_source += " JavaScript!"; '
   'l_source; '
    into l_SOURCE separated by CL_ABAP_CHAR_UTILITIES=>CR_LF.
     
* we compile the JavaScript code
l_JS_PROCESSOR->COMPILE( SCRIPT_NAME = 'DANYTEST.JS' SCRIPT = L_SOURCE ).
     
* Any syntax error ?
if l_JS_PROCESSOR->LAST_CONDITION_CODE <> 0.
   write: / 'Error in COMPILE', l_JS_PROCESSOR->LAST_ERROR_MESSAGE.
   exit.
else.
   write / 'JavaScript was compiled'.
endif.
     
* Let's go and run the script
l_JS_PROCESSOR->EXECUTE( SCRIPT_NAME = 'DANYTEST.JS' ).
     
* Any problem during execution ?
if l_JS_PROCESSOR->LAST_CONDITION_CODE <> 0.
   write: / 'Error in EXECUTE',l_JS_PROCESSOR->LAST_ERROR_MESSAGE.
   exit.
else.
   write / 'JavaScript was executed'.
endif.
     
* we read the output variable
l_RETURN_VALUE = l_JS_PROCESSOR->EVALUATE( JAVA_SCRIPT = 'l_source;' ).
     
write : / l_RETURN_VALUE.

You can code JavaScript directly in your ABAP code.

If you want to do some JavaScript tests in ABAP there is a good program to use : DEMO_JAVA_SCRIPT_MINI_EDITOR. When you run this program, you can code your Java in an ABAP editor, compile and execute it to test the result.

Nowe, here is a small example on how to insert JavaScript in ABAP:

*&---------------------------------------------------------------------*
*& Report ZDANY_JAVASCRIPT_TEST
*&---------------------------------------------------------------------*
REPORT ZDANY_JAVASCRIPT_TEST.
     
data: l_JS_PROCESSOR type ref to CL_JAVA_SCRIPT,
      l_RETURN_VALUE type STRING,
      l_SOURCE       type STRING.
     
* Creation of a new javaScript
l_JS_PROCESSOR = CL_JAVA_SCRIPT=>CREATE( ).
     
* this is the Javascript code
concatenate
   'var l_source = "Hello World,"; '
   'l_source += " I''m"; '
   'l_source += " JavaScript!"; '
   'l_source; '
    into l_SOURCE separated by CL_ABAP_CHAR_UTILITIES=>CR_LF.
     
* we compile the JavaScript code
l_JS_PROCESSOR->COMPILE( SCRIPT_NAME = 'DANYTEST.JS' SCRIPT = L_SOURCE ).
     
* Any syntax error ?
if l_JS_PROCESSOR->LAST_CONDITION_CODE <> 0.
   write: / 'Error in COMPILE', l_JS_PROCESSOR->LAST_ERROR_MESSAGE.
   exit.
else.
   write / 'JavaScript was compiled'.
endif.
     
* Let's go and run the script
l_JS_PROCESSOR->EXECUTE( SCRIPT_NAME = 'DANYTEST.JS' ).
     
* Any problem during execution ?
if l_JS_PROCESSOR->LAST_CONDITION_CODE <> 0.
   write: / 'Error in EXECUTE',l_JS_PROCESSOR->LAST_ERROR_MESSAGE.
   exit.
else.
   write / 'JavaScript was executed'.
endif.
     
* we read the output variable
l_RETURN_VALUE = l_JS_PROCESSOR->EVALUATE( JAVA_SCRIPT = 'l_source;' ).
     
write : / l_RETURN_VALUE.

No comments:

Post a Comment

Content

Recent Topics