Answer the question
In order to leave comments, you need to log in
How to achieve the execution of the script.js file and other scripts when loading the component layout via AJAX?
Hello!
I customize the template of the product comparison component catalog.compare.result as part of the catalog component. When a product is removed from comparison, an ajax request is executed that pulls up the layout of the catalog.compare.result component with updated data.
The stretched layout contains numerous JS decorations, such as slick sliders, which are initialized in the script.js file of the catalog.compare.result component template. When you first open it, everything works as it should, but of course, after removing the product from the comparison, these sliders fall apart, since they are not initialized in the layout fragment returned by AJAX.
In order not to take a lot of time, the question is immediately: is there any most typical, preferred and adequate way to get the code from script.js to work in a situation where the component is loaded/updated via AJAX? Or, if this is not possible, achieve script processing from other files, for example, if the code is placed in the test.js file, and this file is included in template.php:
<!-- ** верстка слайдера ** -->
<script src="<?=$templateFolder?>/test.js"></script>
...
$APPLICATION->ShowAjaxHead(false, false, false, true);
$APPLICATION->IncludeComponent("bitrix:catalog.element", ... );
...
CompareClass.prototype.deleteResult = function(result)
{
var processed = BX.processHTML(result, false);
console.log(processed); // в processed.SCRIPT - пустой массив
BX.closeWait();
BX.onCustomEvent('OnCompareChange');
BX(this.wrapObjId).innerHTML = result;
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question