12.11 Set the Initial Focus in an HTML Page

LANSA provides you with a facility to run a JavaScript function once the HTML page has been loaded by your browser.

By default, LANSA generates a LANSA tag that embeds a Web component as part of the <body> tag.

<body bgcolor="<RDML MERGE="*LW3CLNTCOLOR">"

 background="<RDML MERGE="*LW3CLNTBKGND">" <RDML COMPONENT="FORMINIT">>

This component is known as FORMINIT. This component does not exist in the LANSA component registry, by default. However, this is a facility which can be used to execute a JavaScript function as part of the form loading function by the browser.

To activate this facility, create the FORMINIT Web component as a Text Web component. Define the text to be:

     onload="SetFocus()"

Once this FORMINIT Web component is defined, it means that for every HTML page, it will attempt to execute the SetFocus function as part of the form loading routine by the browser.

 

<body bgcolor="white"

 background="/IMAGES/BACKGRD.GIF" onload="SetFocus()">

The JavaScript function, SetFocus, can be incorporated into your DEFAULT_SCRIPT page. It traverses through all the elements defined in your HTML form, looking for the first instance of an input field or a text area. Once it has found any of these objects, it sets the initial focus to that form element.

 

function SetFocus()

{

   var NumElements=document.LANSA.elements.length;

 

   for (i=0; i<NumElements;i++)

   {

      if (document.LANSA.elements[i].type=="select-one" ||

          document.LANSA.elements[i].type=="checkbox" ||

          document.LANSA.elements[i].type=="textarea" ||

          document.LANSA.elements[i].type=='text')

      {

          if (document.LANSA.elements[i].value!="")

            document.LANSA.elements[i].select();

          document.LANSA.elements[i].focus();

          break;

      }

   }

}