S
S
strify_252020-02-03 17:28:55
1C-Bitrix
strify_25, 2020-02-03 17:28:55

How to include scripts and styles of an ajax-enabled component?

Hello. On the site, when the button is clicked, an ajax request is sent to the ajax.php file, which contains the connection of the prologue and the form component:

<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");?>
<?$APPLICATION->IncludeComponent(
    "custom:forms",
    "callMe",
    Array(
      "FORM_ID" => "callMe"
    )
  );?>

The component template contains the script.js file, which, if there were no ajax, should have been connected automatically. It doesn't happen and the code from the script.js file doesn't work. I read on the Bitrix forum that you need to use the $APPLICATION->ShowAjaxHead() method; , but I did not find any documentation or an example of it. So how do you connect the necessary scripts after the ajax loading of the component?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
ZardoZAntony, 2020-11-03
@ZardoZAntony

after a successful Ajax request in JS:
in res - server response with HTML, which also contains scripts

let processed = BX.processHTML(res, false); // парсим HTML
BX.ajax.processScripts(processed.SCRIPT);  //выполняем найденные скрипты

A
Alexey, 2020-02-04
@AlexeyCaTHaR

Look in the module to see how the method is implemented. Everything is extremely simple and clear
there public function ShowAjaxHead($bXhtmlStyle = true, $showCSS = true, $showStrings = true, $showScripts = true)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question