Lets say I want to get ajax response containing everything that is inside #maincontent selector on catalog/product/view/id/1 page.
The problem is that many JS (that works on product view page) doesn't work when I append ajax response html to my custom page (lets say my custom page is some category view page).
I noticed that scripts (from ajax response html) inside doesnt work. like product gallery. How to make the scripts work? I noticed that after I add a product (using add to cart button from the response) the JS in start to work - gallery becomes visible, related products show up, tabs working etc.
1 Answer 1
It is very easy to solve.
Look in this line: https://github.com/magento/magento2/blob/develop/lib/web/mage/mage.js#L93
Here mage waiting when you insert html content with ajax: "text/x-magento-init".
Example:
you insert to
<div id="gallery"></div>
$('#gallery').html(content)
After call magento:
$('#gallery').trigger('contentUpdated');
-
this makes mage re-evaluate the whole page. what you want is mage to look only into the specified div. is it possible?Claudiu Creanga– Claudiu Creanga2016年07月27日 10:08:00 +00:00Commented Jul 27, 2016 at 10:08
-
I have to use this with products load by infinite ajax scroll. I am using this github.com/manishjoy/magento2-catalog-infinite-scroll but after scroll add to cart button is not working and reload page.Magecode– Magecode2017年09月08日 13:31:15 +00:00Commented Sep 8, 2017 at 13:31
-
2@Magecode, did you got the solution, I am also facing same issueJafar Pinjar– Jafar Pinjar2019年02月04日 14:29:11 +00:00Commented Feb 4, 2019 at 14:29
-
2I am also facing same issue. Any one have a solution?Payal Patel– Payal Patel2021年07月15日 06:41:09 +00:00Commented Jul 15, 2021 at 6:41
-
$('#gallery').trigger('contentUpdated');not workingZahirabbas– Zahirabbas2022年03月21日 13:22:55 +00:00Commented Mar 21, 2022 at 13:22