magento custom CMS page add javascript

magento custom CMS page add javascript

August 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento custom CMS page add javascript

Adding Custom JavaScript to a Magento CMS Page

As a senior developer, I often receive queries about customizing Magento’s CMS functionality. Today, we’ll tackle a common issue – adding specific JavaScript files to a single CMS page without modifying the entire page layout.

The Challenge: Adding NivoSlider JavaScript

Let’s start with the given problem. You want to add the NivoSlider JavaScript library to a custom CMS page created using the official Magento guide. However, you’re facing two main constraints:

  1. Page-specific addition: You need to apply the script only to this particular page, not the entire website.
  2. File-based preference: Preferentially, you’d like to manage the JavaScript file directly in your filesystem rather than through the Magento admin interface.

The Solution: Magento Layout XML and Direct File Inclusion

To resolve these issues, we’ll leverage a combination of Magento’s layout XML updates and direct file inclusion via the layout.local.xml file. Here’s a step-by-step solution:

Step 1: Create the Custom CMS Page

First, ensure you have created your custom CMS page following the official Magento guide (linked in your question). Let’s assume this page is named “About Us” with an identifier of “about_us”.

Step 2: Modify layout.local.xml

Navigate to app/design/frontend/<Your_Theme>/layout/local.xml. If the file doesn’t exist, create it. Add the following content within the <layout> tags:

<layout version="1.0">
    <update handle="default"/>
    <layout>
        <reference name="head">
            <action method="addJs">
                <script>js/nivoslider/jquery.nivo.slider.js</script>
            </action>
            <action method="addCss">
                <stylesheet>css/nivo-slider.css</stylesheet>
            </action>
        </reference>
    </layout>
    <updates>
        <update handle="about_us">
            <reference name="head">
                <!-- Add your custom JavaScript here -->
            </reference>
        </update>
    </updates>
</layout>

Note: Replace <Your_Theme> with the name of your current theme. Ensure that js/nivoslider/jquery.nivo.slider.js and css/nivo-slider.css are located in their respective directories within your theme folder.

Step 3: Update the CMS Page Layout

Next, go to Magento admin –> Content –> Pages –> “About Us” (or your custom page). In the Layout Update XML field, add:

<reference name="about_us">
    <block type="core/text_list" name="about_us.js" as="about_us.js"/>
</reference>

Ensure you save this configuration.

Why This Works

By using layout.local.xml, we create a custom layout handle for our “About Us” page. The <update handle="about_us"> line ensures that any modifications within the <update> tags are applied only to this specific page.

The head reference allows us to add scripts and styles globally within the about_us context without affecting other pages on your site.

Conclusion

In summary, to add JavaScript files directly to a Magento CMS page:

  1. Use layout.local.xml: This allows for precise, page-specific modifications.
  2. Create custom layouts: Define a unique handle (e.g., “about_us”) for each page requiring special treatment.
  3. Reference the head section: Add your scripts and styles within the head reference to ensure they load correctly.

Remember, this approach preserves the flexibility of managing files directly in your theme’s filesystem while keeping modifications isolated to specific pages.

For more advanced Magento customization techniques and best practices, I recommend exploring our comprehensive guides at LaravelCompany.com.

Themes Admin Configuration Errors

Our network