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:


My code:

    <!-- ko ifnot: accountCreated -->
    <p data-bind="i18n: 'You can track your order status by creating an account.'"></p>
        <span data-bind="i18n: 'Email Address'"></span>: <span data-bind="text: getEmailAddress()"></span>
    <form method="post" data-bind="submit: createAccount">
        <input type="submit" class="action primary" data-bind="value: $t('Create Account'), disable: creationStarted" />
    <!-- /ko -->
    <!-- ko if: accountCreated -->
    <p data-bind="i18n: 'A letter with further instructions will be sent to your email.'"></p>
    <!-- /ko -->

<script type="text/x-magento-init">

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
    Jan 16 '17 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 '17 at 14:45

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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