View / Javascript


How to remove history list?

Does anybody know how to remove history list from browser using JavaScript?

[Ted Husted] Alas, it is not permitted. 

Though, you can "break" the history with a redirect -- when they go back the referring page just redirects them again. This at least forces people to view their history list and go back to the page before that.


onSubmit doesn't

Currently I'm working on a form, but before submit, I have an onSubmit() in my form tag that verifies they want to submit. The problem is that with my confirmation javascript popup, whether the user selects "OK" or "Cancel", the form is still submitted. Is there a way to prevent the form from submitting if the user selects "Cancel"?

[Ted Husted] A common Javascript error is to forget to return true or false in your validation script. Here's an example that calls another script called "validateForm()", and  also changes the submit button to an ellipse.

<script language="javascript">
<!--
function submitForm(form) {
if (validateForm(form)) {
form.submit.value=" ..."; 
return true;

else return false;
};
// -->


How do I use Javascript with my Struts forms?

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. 


How do I use Javascript to perform validation in Struts?

[Ted Husted] The best way is using David Winderfeldt's Struts validator servlet. This provides seamless client-side and server-side validation using regular expressions. 

Other than that, you can also attach other Javascript validation to the Struts form fields using the provided properties for Javascript event handling. Of course, these types of validations cannot be guaranteed if a browser has Javascript turned off.

Why doesn't Struts provide it's own facility for regex or Javascript validations?

It's on the TODO list for the 1.1 timeframe


Is it possible to combine dynamic Javascript menu systems with Struts?

[Craig McClanahan] The challenge with doing this is that the JSP tags run on the server (as the page is being generated), while the JavaScript runs on the client side. To integrate the two, you need your tags (and other JSP code) to dynamically generate the JavaScript functions themselves -- sort of having a program write a program -- so that the JavaScript is customized to your particular need on this particular page.

A very trivial example is the way that the <html:form> tag deals with the "focus" attribute. If you specify it, a dynamically generated bit of JavaScript is created to set the input focus, which includes the name of the field you want initial focus
assigned to.

For a more comprehensive scenario, consider that for most menuing systems you will need to configure the list of available options into a JavaScript array or something. You could use the <logic:iterate> tag to render the elements that get set in the array's initialization expression.


Under Netscape 4.7 for Windows, the html:form focus feature doesn't work properly. Is there a work around?

The problem is that the focus feature works all right at first, but when you press tab, it proceeds to the address bar, rather than the next field. After you tab around once, it starts working as expected. One work around is to use the "onblur" event on the initial field to select the second. After that, Bob's your uncle!

Example: 

<html:form action="/logon" focus="username">
<p>Username <html:text property="username"
onblur="this.form.password.focus();"/>
<p>Password <html:password property="password"/>
<p><html:submit property="submit" value="SIGN IN" />

To focus the first field on the first form, you can also do this 

<body  onload="document.forms[0].elements[0].focus();">

in all your form pages rather than use the Struts html:form focus property.