Perl CGI script giving me problems

Andy Wardley abw at wardley.org
Tue Feb 13 13:25:28 GMT 2007


peter church wrote:
> 1) make the cursor appear in the box containing the current question each
> time
> 2) make the web page scroll down to where the box is if the page is longer
> than a standard browser window
> 
> is this easy to achieve with the Perl CGI module?

Yes.  Although as others have pointed out, this is more of an HTML
question than a Perl question.

Just in case you haven't already figured it out, this is the kind of HTML
you require:

   <body onload="document.login.username.focus()">
     <form name="login">
       <input type="text" name="username"/>
     </form>
   </body>

You need to give names to both the form and field that
you want to focus ('login' and 'username' respectively in
this example).

Then you need the Javascript magic incantation:

     document.login.username.focus()

In this example it's embedded in the onload="" attribute of the body
tag.  That's not always the best way to do it, but it'll get you going
for now.

Generating the above using the CGI module is simple.  Just
specified the 'onload' and 'name' arguments to the appropriate
methods.

     #!perl
     use strict;
     use warnings;
     use CGI ':all';

     print start_html(
         -title  => 'Form Focus Example',
         -onload => 'document.login.username.focus()'
     );

     print start_form( -name => 'login' ),
           textfield( -name => 'username' ),
           end_form;

     print end_html();

When the page is displayed (assuming the user has JS available and
enabled), the cursor will automatically be focussed on the form field.
Most (if not all) browsers will scroll down to the field if necessary.

Cheers
A




More information about the london.pm mailing list