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?

2
  • Please, describe your problem. I think, that we don't need to observe loading of template file on this abstraction level.
    – Max
    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!) 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>
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.