Getting started with Haxe/JS

Developing Javascript code is really easy with Haxe. Let's see our first HelloWorld example :

class Test {
    static function main() {
        trace("Hello World !");
    }
}

Put this class into a file named Test.hx and create the file compile.hxml in the same directory with the following content :

-js test.js
-main Test

To compile, you can simply double-click on the compile.hxml file (or run the command haxe compile.hxml). If an error occurs, it will be displayed. If everything went smoothly like it should, this will produce a file named test.js that can be embedded into an HTML page such as this one :

<html>
<head><title>Haxe JS</title></head>
<body>

<div id="haxe:trace"></div>
<script type="text/javascript" src="test.js"></script>

</body>
</html>

If you put this code into a file named test.html and open it with your webbrowser, it should display Hello World with information about the file and the line where the trace occured. Please note that a div block is needed to display the Haxe Javascript traces.

This example does not cover package often used to help organise the classes in your project.

Using Alert

Another possibility to display some text is to use alert. Modify Test.hx with the following content :

class Test {
    static function main() {
    // On HaXe 2.x
        js.Lib.alert(js.Lib.window.location.href);
    // On HaXe 3.x
        js.Lib.alert(js.Browser.window.location.href);
    }
}

Compile and open the test.html. It should display a window message box displaying the current location URL.

Watch the available APIs from Lib .

version #19708, modified 2013-08-25 04:46:51 by SrMouraSilva