package e4s.tutorial; import e4s.html.*; import e4s.servlet.E4ModuleImplementation; /** * E4JavaScript and event-handler examples. Java-Scripts can be used wherever required, * Event-Handlers are "bound" to their base types, e.g. has the E4EventHandlerBODY * functionality for bindings with onLoad() or onUnLoad(). Scripts as well as * event handlers can either have an explicit name or they get assigned an * generic name. Scripts are collected throught the object-tag chain within * the HTML output (except Ajax, it always starts on HTML level) and included * in the header section of the HTML file. It is also possible to use referenced * scripts (stored outside as .js file), but the elegance is the possibility * to include certain topics in the script, for example a localized text message. * * {@tutorial Example_E4JavaScript} */ public class Example_JavaScript extends E4ModuleImplementation { public static E4Method start = null; public void start(HTML html) { BODY body = html.BODY(); body.Message(E4Message.INFO, "Java-Script handling"); // This is an "unnamed" script which is implementes as E4EventHandler, // we can execute but not reference later on it. The handler will // be added explicit to the body element E4EventHandlerBODY loading = new E4EventHandlerBODY(E4EventHandlerBODY._EVENT_ONLOAD); loading.appendln("function " + loading.getName() + "()"); loading.appendln("{"); loading.appendln(" alert('This unnamed script was called on loading the page');"); loading.appendln("}"); body.setEventHandler(loading); // This is an "unnamed" script and allready associated with an onLoad() event in // the body tag. E4EventHandlerBODY exit = body.onUnLoad(); exit.appendln("function " + exit.getName() + "()"); exit.appendln("{"); exit.appendln(" alert('So long..');"); exit.appendln("}"); body.P(); // this is a named script E4JavaScript js = new E4JavaScript("myScript"); js.appendln("function " + js.getName() + "(txt)"); js.appendln("{"); js.appendln(" alert(txt);"); js.appendln("}"); // don't forget to add it explicitly body.addScript(js); E4EventHandlerA onClick = new E4EventHandlerA(E4EventHandlerA._EVENT_ONCLICK); onClick.setParam("on the text"); onClick.appendln("function " + onClick.getName() + "(x)"); onClick.appendln("{"); onClick.appendln(" alert('You clicked on ' + x);"); onClick.appendln("}"); A href = body.A(onClick); href.println("Click here"); body.P(); // this is an unnamed script E4JavaScript butterfly = new E4JavaScript(); butterfly.appendln("function " + js.getName() + "(txt)"); butterfly.appendln("{"); butterfly.appendln(" alert('you clicked the butterfly');"); butterfly.appendln("}"); IMG img = body.IMG("images/tutorial/Example_A/butterfly01.jpg"); img.setScript((E4EventHandler_Intf)onClick); } }