Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Add external javascript in html view in cl_gui_custom_container

0 Kudos

hi everyone,

i have a question, how i make a report with html in SAP adding external javascript files in a html?, i loaded data using a method in this report, 

REPORT z_load_html.

CLASS make_html DEFINITION.
  PUBLIC SECTION.
    METHODS init.

  PRIVATE SECTION.
    TYPES: typ_file_line(1022) TYPE x,
           typ_file_tab        TYPE STANDARD TABLE OF typ_file_line.

    DATA: cl_html       TYPE REF TO cl_gui_html_viewer.

    METHODS:
      convet_xstring_to_filetab
        IMPORTING lv_xstring TYPE xstring
        EXPORTING lt_filetab TYPE typ_file_tab.
ENDCLASS.

CLASS make_html IMPLEMENTATION.
  METHOD init.
    DATA: lv_html    TYPE string,
          lv_script  TYPE string,
          lv_x_html  TYPE xstring,
          lv_xscript TYPE xstring,
          lt_html    TYPE typ_file_tab,
          lt_script  TYPE typ_file_tab.

    DATA: cl_container TYPE REF TO cl_gui_custom_container.

    " Print Alert
    lv_script =
      'd2luZG93LmFsZXJ0KCdzY3JpcHQgd29yayEnKTs='.

    " simple HTML
    CONCATENATE
      'PCFET0NUWVBFIGh0bWw+PGh0bWwgbGFuZz0iZW4iPjxoZWFkPj'
      'xtZXRhIGNoYXJzZXQ9IlVURi04Ij48bWV0YSBuYW1lPSJ2aWV3'
      'cG9ydCIgY29udGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoLCBpbm'
      'l0aWFsLXNjYWxlPTEuMCI+PHRpdGxlPkRvY3VtZW50PC90aXRs'
      'ZT48L2hlYWQ+PHNjcmlwdCBzcmM9InNjcmlwdC5qcyI+PC9zY3'
      'JpcHQ+PGJvZHk+PGRpdj48L2Rpdj48L2JvZHk+PC9odG1sPg=='
    INTO lv_html.

    " Convert B64 to XSTRING
    cl_http_utility=>if_http_utility~decode_x_base64(
      EXPORTING
        encoded = lv_html
      RECEIVING
        decoded = lv_x_html
    ).

    cl_http_utility=>if_http_utility~decode_x_base64(
      EXPORTING
        encoded = lv_script
      RECEIVING
        decoded = lv_xscript
    ).

    " Mont HTML view and Files
    convet_xstring_to_filetab(
      EXPORTING lv_xstring = lv_x_html
      IMPORTING lt_filetab = lt_html
    ).

    convet_xstring_to_filetab(
      EXPORTING lv_xstring = lv_xscript
      IMPORTING lt_filetab = lt_script
    ).

    " init HTML view
    CREATE OBJECT cl_container
      EXPORTING
        container_name = 'HTML_CONTAINER'.

    CREATE OBJECT cl_html
      EXPORTING
        parent = cl_container.

    cl_html->LOAD_DATA(
      EXPORTING
        url        = 'index.html'
        type       = 'text'
        subtype    = 'html'
      CHANGING
        data_table = lt_html
    ).
    cl_html->LOAD_DATA(
      EXPORTING
        url        = 'script.js'
        type       = 'text'
        subtype    = 'js'
      CHANGING
        data_table = lt_script
    ).

    cl_html->show_daTA(  url = 'index.html' ).

    call SCREEN 9000.

  ENDMETHOD.

  METHOD convet_xstring_to_filetab.


    DATA: lv_len TYPE i,

          lv_j   TYPE i.

    lv_len = xstrlen( lv_xstring ).

    lv_j  = 0.

    CLEAR lt_filetab.

    WHILE lv_j < lv_len.
      IF lv_j + 1022 < lv_len.
        APPEND lv_xstring+lv_j(1022) TO lt_filetab.
      ELSE .
        APPEND lv_xstring+lv_j TO lt_filetab.
      ENDIF.

      ADD 1022 TO lv_j.
    ENDWHILE.
  ENDMETHOD.                    "convet_xstring_to_fileTab

ENDCLASS.

INITIALIZATION.
  DATA cl_class TYPE REF TO make_html.

  CREATE OBJECT cl_class.
  cl_class->init( ).

on IE11 the result open perfectly.

caio_dh_consultig_0-1707328369585.png

 


however, i don't display alert in EDGE browser controller there are strange charateres in javascript text of include and edge don't read script, someone help-me in this problem?

caio_dh_consultig_1-1707328805903.png

 

 

 

2 REPLIES 2

0 Kudos

i found the problem the xstring table is bigger of the lenght of xstring, there are '0' to complete line table, and this create a Null in text.

caio_dh_consultig_0-1707416999606.png

 

0 Kudos
    cl_html->load_data(
      EXPORTING
        url        = 'script.js'
        type       = 'text'
        subtype    = 'js'
        size       = lv_len
      CHANGING
        data_table = lt_script
    ).

In the end is was pass to var the lenght o of size and this resolve my problem,