I am making changes to my Magento 2 cart.
I want to run a particular JavaScript function once the registration.html file is loaded
My file:
/app/design/frontend/mytheme/default/Magento_Checkout/web/template/registration.html
My code:
<div>
<!-- ko ifnot: accountCreated -->
<p data-bind="i18n: 'You can track your order status by creating an account.'"></p>
<p>
<span data-bind="i18n: 'Email Address'"></span>: <span data-bind="text: getEmailAddress()"></span>
</p>
<form method="post" data-bind="submit: createAccount">
<input type="submit" class="action primary" data-bind="value: $t('Create Account'), disable: creationStarted" />
</form>
<!-- /ko -->
<!-- ko if: accountCreated -->
<p data-bind="i18n: 'A letter with further instructions will be sent to your email.'"></p>
<!-- /ko -->
</div>
<script type="text/x-magento-init">
alert('yes');
</script>
I want it so that once that file is loaded via AJAX it runs the alert.. I have tried just using <script> and <script type="text/javascript"> but it is not working. Please can you help?
-
Please, describe your problem. I think, that we don't need to observe loading of template file on this abstraction level.Max– Max2017年01月16日 10:16:58 +00:00Commented Jan 16, 2017 at 10:16
-
Please can you show me how to write this and what files I need - I want to execute some javascript external function once this file is loaded.. the problem is.. all HTML files load after the dom (bloated Magento!)TheBlackBenzKid– TheBlackBenzKid2017年01月16日 14:45:05 +00:00Commented Jan 16, 2017 at 14:45
1 Answer 1
If you need to work with DOM elements, rendered by knockout from this template, you can use afterRender binding.
<div data-bind="afterRender: function() {alert('yes');}"></div>
-
1That is fantastic! So if that element is pulled via AJAX it will automatically fire. That is pretty nice.TheBlackBenzKid– TheBlackBenzKid2017年01月17日 05:59:10 +00:00Commented Jan 17, 2017 at 5:59
-
Thx for the answer! For anyone that needs it, here is the link to the Magento 2 KnockoutJS bindings DevDocs: devdocs.magento.com/guides/v2.2/ui_comp_guide/concepts/…Jonathan Marzullo– Jonathan Marzullo2018年05月07日 15:34:00 +00:00Commented May 7, 2018 at 15:34