JSXHelloWorld @Jun 7, 2007, 2:40:59 PM

About This Tutorial

This "HelloWorld" tutorial has been divided into two sections;

  1. In the first section you will learn how to properly include JSX onto your page.
  2. Second section will show you how to build your first JSX module that captures a mouse click on a button and pop-up an alert.

Enable JSX on HTML page

In this section you will essentially test your JSX installation. First create a 'index.html' page in your context root - the place where you've installed the JSX libraries. To actually enable JSX Toolkit on your page, insert two <script> tags into the head element.

  • First tag defines global variables that configure the JSX.
    • jsxContext variable defines the path to the directory where JSX root is.
    • jsxLogLevel variable is optional and sets the level of debug logging.
  • Second tag loads the JSX core library.
<html>
    <head>
        <script type="text/javascript">var jsxContext='.'; var jsxLogLevel='INFO';</script>
        <script type="text/javascript" src="./jsx/core.jsx"></script>
    </head>
    <body>
        <script type="text/javascript">
	    logger.info("Welcome to JSX!");
	</script>
    </body>
</html>

That's it! With above code in your page, you should be able to see a popup log window appear and log, that should be clear indication that JSX is enabled.

First JSX package

Everything in your JSX enabled application should be done by packages. Some other frameworks create one monolithic .js file that holds everything, but this is not our way. The advantage of this approach is that you can load only those parts of JSX Toolkit (and your packages) that the page actually needs. This saves bandwidth, and of course, user frustration with slowly loading pages. If you want to write all your application code inside the head tags, be our guest.

So, let's get started.

  • First, in the same directory that holds the index.html file from above (and hopefully the jsx), create a directory myapp.
  • Inside the myapp directory create a 'mypackage.jsx' file.
  • Paste following code into the newly created file.
jsxPackage("myapp.mypackage", function(thisPackage){

	var EventHandler = jsxImport("jsx.utils.events.EventHandler");

	jsxClass("MyClass", EventHandler, function(superClass){
	
		this.instantiate = function(element){
			superClass.instantiate.call(this);
			this.addHandledElement(element);
		}
		
		this.onclick = function(evt){
			alert("Hello World!");
		}
	});
});

The jsxClass that was created is going to live inside the myapp.mypackage.MyClass namespace. The MyClass class is an EventHandler, it hooks up to the JSX event routing engine and will receive events that are destined for an element we are going to attach it to.

<html>
    <head>
        <script type="text/javascript">var jsxContext='.'; var jsxLogLevel='INFO';</script>
        <script type="text/javascript" src="./jsx/core.jsx">
    </head>
    <body>
        <input type="button" id="ClickMe" name="ClickMe" value="ClickMe">
        <script type="text/javascript">jsxBehavior("myapp.mypackage.MyClass", "ClickMe");</script>
    </body>
</html>