Jaxcent also allows other mechanisms for working with JavaScript.
The method execJavaScriptCode in JaxcentPage
class executes JavaScript code. It can either be given just
some chunk of code, or a function name that takes 1 or more
arguments. For example:
execJavaScriptCode( "alert( \"Hello\" );", false, null );
or
execJavaScriptCode( "alert", false, new Object[]{ "Hello" } );
The arguments are specified as an Object-array, and besides
the primitive type-wrappers (Integer, Boolean etc) and String,
can also include Jaxcent HTML element objects e.g. HtmlInputText,
HtmlPara etc.
The second parameter is false in these examples.
If it is specified as true, the arguments are
passed as a single parameter to the JavaScript function.
The single parameter is a JavaScript array containing
the actual list of arguments.
In addition to execJavaScriptCode, there
is an evalJavaScriptCode method, which
is similar but waits for the result from the JavaScript
evaluation, and returns the result.
Jaxcent provides the JavaScript function JaxcentServerRequest
to the client side. This function can be called with 0 or more parameters
by JavaScript code on the client side.
Jaxcent provides the corresponding Java method onJavaScriptRequest
in the JaxcentPage class. This method can be
over-ridden to process messages coming from JaxcentServerRequest. It will
be called with two null parameters, or a command and optionally an array of arguments.
Exercise:
Create an HTML file Jstest.html, with the Jaxcent JavaScript include statement in it.
Also add the following JavaScript code in the HEAD section.
<SCRIPT LANGUAGE="JavaScript">
<!--
function identify( element )
{
oldColor = element.style.backgroundColor;
elementToReset = element;
element.style.backgroundColor = "red";
setTimeout( "resetIdentified()", 1000 );
}
function factorial( n )
{
if ( n < 2 ) {
return 1;
}
return n * factorial( n-1 );
}
var oldColor;
var elementToReset;
function resetIdentified()
{
elementToReset.style.backgroundColor = oldColor;
}
var iter = 1;
function callServer()
{
JaxcentServerRequest( "Test", "Calling", "Server", iter++ );
setTimeout( "callServer()", 10000 );
}
setTimeout( "callServer()", 10000 );
-->
</SCRIPT>
identify" can be called with an HTML element as
argument. It will identify the element on the page by briefly turning it red.
Add a Java handler to the first button. In the handler, call the JavaScript
"identify" function to identify the INPUT TEXT field.
factorial" with the argument "5" and print
out the result in the output window.
| Next Step: Hiding and Showing Page Elements |