7

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?

asked Jan 16, 2017 at 7:35
2
  • Please, describe your problem. I think, that we don't need to observe loading of template file on this abstraction level. Commented 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!) Commented Jan 16, 2017 at 14:45

1 Answer 1

18

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>
answered Jan 16, 2017 at 15:02
2
  • 1
    That is fantastic! So if that element is pulled via AJAX it will automatically fire. That is pretty nice. Commented 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/… Commented May 7, 2018 at 15:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.