Show a loading message for a Magento Ajax request?

Show a loading message for a Magento Ajax request?

October 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Show a loading message for a Magento Ajax request?

Displaying Loading Messages for Magento Ajax Requests

When your Magento site is making an AJAX request on the frontend, it’s often useful to display a loading message or spinner to provide visual feedback to users that something is happening behind the scenes. Here’s how you can achieve this using Magento’s built-in JavaScript functionality.

Understanding Magento’s Loading Mask

Magento comes with a pre-built loading mask component that includes an orange background and a spinning GIF animation. This is controlled by the mage/loading-mask module, which is part of the core JavaScript files in your theme.

To use this loading mask during your AJAX requests, you need to ensure it’s loaded correctly on your page first. Here are the steps:

  1. Check if the loading mask is included: In your theme’s requirejs-config.js file, look for the following configuration under the paths section:

    "mage/loading-mask": "js/loading-mask",
    

    If it’s not there, add this line to ensure the loading mask JavaScript file is included in your page build.

  2. Include the CSS: Make sure you have the necessary CSS files for the loading mask. In your theme’s layouts directory, create or edit the default.xml layout file and include the following:

    <page level="1">
      <css src="Magento_Theme::mage/loading-mask/loading-mask.css"/>
    </page>
    

Using the Loading Mask in Your AJAX Requests

Once you have set up the loading mask, using it is straightforward. Here’s how to include it in your JavaScript function that makes the AJAX request:

require([
    'jquery',
    'mage/loading-mask'
], function($) {
    var url = '/your Ajax url';
    $.ajax({
        type: "POST",
        dataType: "json",
        showLoader: true, // This line enables the loading mask
        url: url,
        success: function(result) {
            console.log('Success: ', result);
        },
        error: function(errorThrown) {
            console.error('Error: ', errorThrown);
        }
    });
});

The key here is to include showLoader: true in your AJAX options. This signals the Magento script to show the loading mask during the request.

When to Use Loading Masks

Remember, loading masks should be used judiciously. They’re most effective when:

  • The user expects a delay (like a search or form submission).
  • The delay is perceivable by the user.
  • There’s no other visible feedback on what’s happening.

Overusing them can create an inconsistent user experience. Always test your implementation to ensure it enhances, rather than hinders, the user experience.

Conclusion

Adding loading messages to Magento AJAX requests isn’t complex once you understand how to use the built-in components. By correctly setting up the mage/loading-mask module and including it in your JavaScript functions, you can provide a more polished, responsive feel to your site’s interactivity.

Remember, keeping users informed is key to maintaining their trust and engagement with your store. Learning to leverage Magento’s built-in tools like this loading mask is just one way to improve the user experience on your e-commerce site.

For more tips on optimizing your Magento performance, check out our guide on Speeding Up Your Magento Site.

Extensions Themes Configuration JavaScript

Our network