Navigation: For Developers > Extend Functionality with HEScript > Sample Scripts > How to call a javascript function from HEScript (toolbar, menubar)? |
|
This guide explains how to call JavaScript functions from a button, menu item, or any general HEScript code in HTML Executable. Let's get started!
The Objective 🎯
You want to call a JavaScript function from your button, menu item, or any general HEScript code. How can you do that? The answer is simple: use the built-in HEScript function named `ExecuteHTMLScript`.
Step-by-Step Guide 📝
Step 1: Open Your HTML Executable Project 📂
Open your HTML Executable project and navigate to the Script Manager. Double-click the "UserMain" script to open the script editor:
Step 2: Use the ExecuteHTMLScript Function 🖋️
In the script editor, use the following code if you want to call your JavaScript code in the default homepage at startup:
procedure OnStartMainWindow;
begin
// When the main window is going to be displayed (just before the homepage is shown).
ExecuteHTMLScript("sayHello('world')","");
end;
Don't forget to save your script!
Step 3: Include the Correct JavaScript Function 📚
Ensure that you have the correct JavaScript function in the default homepage (or any HTML page). For instance:
<script>
function sayHello(name) {
alert('Hello ' + name + '!');
}
</script>
Final Step: Compile Your Project 🧪
Compile your project and that's it! You've successfully called a JavaScript function from your HTML Executable project.