hakkr has asked for the wisdom of the Perl Monks concerning the following question:

When returning a web page from a CGI script I would like to be able to scroll the page down. I want to do this so the user can see the correct part of the form which is out of their view at the bottom of the page. I've tried using javascript to reload the page after the CGI prints to make it jump to an anchor but with no joy. I know you can link to anchors within static pages by appending #anchorname to the URL. In testing many users have not realised they have to scroll down, I need to hold their hand and stick the right part of the page in their face.

Cheers

Replies are listed 'Best First'.
Re: Anchor CGI output
by cfreak (Chaplain) on Feb 05, 2002 at 15:21 UTC

    You should be able to append the anchor name to the end of the URL string just like the static pages. For example:

    http://mydomain.com/cgi-bin/somescript.cgi?param=whatever#anchor

    I know I've anchored CGI scripts before. But I can't remember if I've done it while passing parameters. If you are passing parameters like that and it doesn't work try passing them with a POST method instead

Re: Anchor CGI output
by Biker (Priest) on Feb 05, 2002 at 14:55 UTC

    This is totally untested. Shooting from the hip here.

    Have you tried to put the anchor in the referencing URL?
    Like: http://yoursite/yourscript#anchor?yourparameters

    "Livet är hårt" sa bonden.
    "Grymt" sa grisen...

Re: Anchor CGI output
by jarich (Curate) on Feb 05, 2002 at 22:44 UTC
    Could you reorder the output perhaps? The most important details on any web page should be where the user will look for them (which is usually up the top). If you're having problems with users not scrolling down then you're having problems with your page design.

    Jumping the user down to the correct spot means that whatever is at the top of the page isn't important anyway.

    jarich

Re: Anchor CGI output
by dws (Chancellor) on Feb 06, 2002 at 02:16 UTC
    Here's a quick-and-very-dirty demo of how to cause scrolling once a page has loaded. If you don't like JavaScript, avert your eyes now.

    The idea is emit a page that includes an anchor at the bottom. Along the way we emir an onLoad handler that forces the document's location to that anchor.

    The fragment below works on IE. To work on Netscape requires adding some browser detection logic.

    #!/usr/bin/perl print <<START; Content-type: text/html <html> <head> <title>test</title> <script language="JavaScript"> function scrollDown() { top.location = "#end" // document.URL = "#end" for Netscape } </script> </head> <body onLoad="scrollDown()"> START print "stuff<br>\n" for ( 1 .. 100 ); print <<FINISH; <a name="end">End </body> </html> FINISH
Re: Anchor CGI output
by Cody Pendant (Prior) on Feb 05, 2002 at 22:25 UTC
    You say you've tried doing it with JavaScript but no joy -- care to elaborate on how? I'm sure it can be done with a document.location.href='documentname.html#anchor'; statement or similar. Do that with an onLoad() handler and you can be sure it won't trigger too early. JavaScript has got anchors pretty much covered.

    Also, why not put a META-refresh into the page which would do the same thing? Doesn't require JS and does the same job. The META is supposed to be in the head, so you've got the too-early thing but it's timeable, and I bet most browsers would happily follow it if it came up anywhere in the document anyway.
    --

    ($_='
      jjjjjjuuuuuuuuusssssssssttttttttt annnnnnoootttttthhhhheeeeeerrrrrr
      pppeeeerrrrrrrrrrrrllllllllllllll haaaaccccccckkkkkkkeeeeeeeerrrrrr')
                   =~y/[a-z]//s;print;
Re: Anchor CGI output
by hakkr (Chaplain) on Feb 05, 2002 at 16:01 UTC

    Sorry guys my verbalisation of this is obviously a little below par.

    It the is output I'm printing from my CGI script I want to anchor.

    i.e When I print an HTML template to STDOUT or use a print statement to output my page.

    Oh well it probably can't be done server side. cheers

    UPDATE: The Javascript from dws works well. I also discovered I can put anchorlinks in my form actions for the same effect. i.e <form name="" method="post" action="/cgi-bin/script.cgi#anchorname">

      "I also discovered I can put anchorlinks in my form actions for the same effect."

      That's exactly what I tried to tell you, but obviously failed to explain very well. ;-)

      "Livet är hårt" sa bonden.
      "Grymt" sa grisen...