How do I use Javascript with my Struts forms?

Ted Husted

The relevant HTML tags provide support for the Javascript event handlers onblur, onchange, onclick, ondblclick, onfocus, onkeydown, onkeypress, onkeyup, onmousedown, onmousemove, onmouseout, onmouseover, onmouseup.

For more about Javascript events, see the Netscape Javascript Reference guide.

For more about HTML tags, see the Struts HTML Tag Guide.

As usual, the Javascript itself can then be placed:

  1. Inline, if it is simple enough, like onclick="history.go(-1)" 
  2. As a <SCRIPT> within your JSP 
  3. As an included <SCRIPT src="my.js"> to your JSP 

Note that the Struts Javascript properties use all lowercase identifiers, for XML compatibility.