#
App Initialization
In h5, just like in a standard .NET console application, the entry point is the static void Main() method. When the browser loads the generated JavaScript, h5 automatically executes the Main method of your application class.
#
The Main Method
To define an entry point, create a class with a static Main method.
using System;
using H5;
namespace MyProject
{
public class Program
{
public static void Main()
{
Console.WriteLine("Hello from h5!");
}
}
}
When compiled, h5 generates JavaScript that calls MyProject.Program.Main() as soon as the scripts are loaded and the DOM is ready (if configured).
#
The [Ready] Attribute
The [Ready] attribute can be applied to any static method to indicate that it should be executed when the DOM is fully loaded. This is useful for initializing UI components or setting up event listeners.
using H5;
namespace MyProject
{
public class App
{
[Ready]
public static void OnReady()
{
// executed when DOMContentLoaded event fires
Console.WriteLine("DOM is ready!");
}
}
}
You can have multiple methods with [Ready]. The execution order is not guaranteed, so avoid dependencies between them if possible.
#
The [Init] Attribute
The [Init] attribute marks a static method to be executed immediately when the script is loaded, before [Ready] and before Main. This is useful for configuring libraries or setting up global state that doesn't depend on the DOM.
using H5;
namespace MyProject
{
public class Config
{
[Init]
public static void Initialize()
{
// executed as soon as the script is evaluated
Console.WriteLine("Script loaded, initializing configuration...");
}
}
}
Execution Order:
[Init]methods[Ready]methods (onDOMContentLoaded)Mainmethod
#
Customizing Initialization
If you need more control over initialization, you can disable automatic execution in h5.json and manually call your entry point from JavaScript. However, the default behavior covers most use cases.
#
Using h5.json
You can control whether the Main method is automatically called using the autoRun option in h5.json (if available in your version) or by simply omitting the Main method and using [Ready] instead.
{
"output": "h5/",
"html": {
"disabled": false
}
}
By default, if html generation is enabled, h5 generates an index.html that includes your scripts and triggers the initialization sequence.