Odoo integrates both Python and JavaScript to offer complete functionality. Python is responsible for backend logic, while JavaScript enhances the user interface and client-side interactions. In this blog, we’ll explore how to load a JavaScript function upon clicking a menu item in Odoo 18.
To begin,
Create the Menu Item (menu.xml): Define a menu item in an XML file that triggers the JavaScript function when clicked.
menu.xml
Define the Action (actions.xml):
Create a client action in an XML file corresponding to the menu item. This Action will specify the tag that links to the JavaScript function.
actions.xml
Load JS Code
js_code
Implement the JavaScript Function (script.js): Write the JavaScript code executed when the Action is triggered. This script imports necessary modules, registers the Action, and defines the execution function.
script.js
/** @odoo-module **/
import { registry } from "@web/core/registry";
const actionRegistry = registry.category("actions");
const loadJsAction = async (env,context) => {
// Custom JS Code Here
console.log('JS File Loaded === Custom Script Will Execute Here',env,context);
window.alert('Sidmec Technologies Pvt Ltd');
};
actionRegistry.add("js_code", loadJsAction);
Configure the Manifest File (__manifest__.py): Update the manifest file to include the JavaScript file in the backend assets and reference the XML files.
__manifest__.py
'assets': {
'web.assets_backend': [
'module_name/static/src/js/script.js',
],
},
'data': [
'views/actions.xml',
'views/menu.xml',
],
This process ideally integrates a JavaScript function into an Odoo 18 menu item click, enhancing the application’s interactivity.
Use cases:
Dynamic Data Loading:
Description: Load data dynamically from an external API or server when clicking a specific menu item.
Example: Fetch and display the latest sales statistics or real-time inventory levels.
Interactive Dashboards:
Description: Trigger interactive elements or widgets in the dashboard.
Example: Show a custom chart or graph visualizing data relevant to the selected menu item.
Conditional Logic Execution:
Description: Execute conditional logic based on user actions.
Example: Display different content or trigger actions based on user roles or permissions.
Final Thoughts
Integrating JavaScript functionality with menu item clicks in Odoo can make your application more dynamic and responsive to user actions. Following this guide, you can easily implement custom JS functions that enhance the user experience.



