Friday, July 1, 2011

Struts html:form with focus element creates JS error on IE

The built in struts <html:form ...  focus="[elementname]"> functionality automatically generates this X-browser

you can see this on your browser at the end of form element.

<script type="text/javascript" language="JavaScript">
 var focusControl = document.forms["component"].elements["contents"];
 if (focusControl.type != "hidden") {
 // -->

With this, Internet Explorer (version 6, 7 and 8) generates the following error:

"Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus."

The problem seems to be that for some very odd reason the javascript is being called before the element is available.

So, to comeout of this, I had removed the struts generated focus field(form tag) and added a own script., below the form with a try/catch.

Something like this..

+<script type="text/javascript" language="JavaScript">
+ var focusControl = document.forms["component"].elements['<c:out value="${focus}"/>'];
+ try{
+ if (focusControl.type != "hidden")
+  focusControl.focus();
+ }catch(er){
+  //Fix for IE focus problem.
+ }

Now the error goes off...

No comments:

Post a Comment