Add external JavaScript to Magento backend

Add external JavaScript to Magento backend

August 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add external JavaScript to Magento backend

Adding External JavaScript to Magento Backend: A Comprehensive Guide

The Goal

The primary objective is to link Google’s jQuery file externally on every backend site of your Magento installation.

My Initial Attempt

In an effort to achieve this, I attempted to add the following code to both local.xml and main.xml within the design/adminhtml/ directory:

<reference name="head">
  <block type="core/text" name="google.cdn.jquery">
    <action method="setText">
      <text><![CDATA[<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>]]></text>
    </action>
  </block>
</reference>

I believed this would work because a similar approach does function in the frontend (design/frontend/ directory). However, I encountered a significant issue.

The Problem with My Attempt

To my dismay, nothing happened. There were no errors or warnings; it was as if the addition had absolutely no effect whatsoever. Regardless of whether I included the code or not, there was no noticeable change in the backend’s behavior.

This led me to conclude that the standard Magento approach for linking JavaScript files in the frontend does not directly translate to the admin backend context.

The Correct Approach

Upon further investigation and consultation with Magento experts, it became apparent that a different strategy is required when working with external JavaScript files in the Magento admin panel.

The solution lies in creating a custom module. Here’s an outline of the necessary steps:

  1. Create a new XML layout file inside your custom module’s app/code/local/YourCompany/YourModule/view/adminhtml/layout directory. Name this file something like yourmodule.xml.

  2. In the XML layout file, add the following code to include the external jQuery library:

<layout version="0.1.0">
  <default>
    <reference name="head">
      <block type="magentothem/yourmodule" template="yourmodule/yourjquery.phtml"/>
    </reference>
  </default>
</layout>

Replace 'magentothem/yourmodule' with the actual namespace and module name, and 'yourjquery.phtml' with a new PHTML file that will contain your JavaScript.

  1. Create the yourjquery.phtml file in the appropriate directory (app/code/local/YourCompany/YourModule/view/adminhtml/templates/yourmodule/).

  2. In this PHTML file, add the following code to include the external jQuery library:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  1. Compile your Magento store’s static files by running these commands in the terminal:
php bin/magento setup:static-content:deploy -f
rm -rf pub/static/adminhtml/**
php bin/magento setup:static-content:deploy -f
  1. Clear the cache and refresh your admin panel to see the changes.

Conclusion

Adding external JavaScript files to the Magento backend requires a more advanced approach than simply appending an XML block to the layout file, as I initially attempted. The correct path involves creating a custom module with a dedicated PHTML template for the JavaScript inclusion.

This method ensures that your external library is properly loaded in the admin panel without causing conflicts or unexpected behavior. Always remember to follow best practices when working with Magento’s XML layout and block system, especially when dealing with third-party scripts.

Database API Themes Admin

Our network