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

Hi, I have successfully written a cgi scripts which prints a load of processed results onto the web. However, i would like to give the page some character e.g. drop down menus etc. Is it possible to combine a cgi script with javascript to achieve this?

Replies are listed 'Best First'.
Re: cgi and javascript
by jeffa (Bishop) on Jun 23, 2005 at 15:15 UTC

    Try something like this:

    use strict; use warnings; use CGI::Pretty qw(:standard); print header, start_html( -title => 'CGI.pm and Javascript', -script => { -language => 'JAVASCRIPT', -code => q| function launch(select) { if (!select.value) return false; window.location.href = select.value; return false; } |, }, ), start_form, popup_menu( -name => 'jump_box', -onChange => 'launch(this)', -values => [qw( http://google.com http://perlmonks.com http://imdb.com )], ), end_form, end_html, ;

    UPDATE: sigh ... it's just an example dorward. Personally, i see nothing wrong with using Javascript for such means, even though some people don't use Javascript or use only their keyboards. The OP asked for drop down menus with Javascript. This is an application of such. Personally, i would much rather use HTML::Template or Template Toolkit than CGI.pm ... but this is a working example of how to do it with CGI.pm.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      I really wouldn't advise that, but Jukka explains why better then I can.

      Update: And there are plenty of ways to implement it in a fashion which is not so fragile (and, depending on jurisdiction and circumstances, potentially illegal). The article I referenced describes how to implement it as such.

Re: cgi and javascript
by holli (Abbot) on Jun 23, 2005 at 15:10 UTC
    Javascripts are part of the html, so all you have to do is to print out the scripts along with your html. Or did I miss something?


    holli, /regexed monk/
Re: cgi and javascript
by blazar (Canon) on Jun 23, 2005 at 14:52 UTC
    Yes!

    Seriously: not much to add. They're (mostly) orthogonal concepts.

Re: cgi and javascript
by goober99 (Scribe) on Jun 23, 2005 at 16:06 UTC
    You can use XMLHttpRequest to actually have a client-side JavaScript send and receive data from a server-side Perl script without refreshing the page. It is actually really easy to implement and allows you to do a lot of cool things.
Re: cgi and javascript
by fmerges (Chaplain) on Jun 23, 2005 at 16:57 UTC

    Hi,

    Some options:

    You can print out the javascript code like the html, or along with it.

    You can use the facility of CGI.pm to insert the javascript code with a method call, but this is not really necessary.

    Perhaps for me the best option is to put this code outside the cgi and only add a tag that makes reference to this code. And using some templating facility, like TT, Mason, etc.

    And on the way, getting a look at AJAX or XMLHttpRequest can be nice, but I think this is for a next step, once you get familiar with the web driven programming...

    Regards,

    :)
Re: cgi and javascript
by anonymized user 468275 (Curate) on Jun 23, 2005 at 15:04 UTC
    As far as I understand it, you can call javascript from cgi by having it issue the appropriate html to call the java script, e.g.:
    print '<input type="text" name="whizzbangGadgets" size="5" maxlength=" +4" onblur="checkValue(this, 150);MyJavaGadgets()>';

    -S

Re: cgi and javascript
by TedPride (Priest) on Jun 23, 2005 at 17:11 UTC
    Yes, you can write Javascript from Perl, just like any other page content. Work out what your Javascript code will be in the page and which parts will need to be generated by the script, and cycle through your content for those parts. Of interest will be printing in blocks:
    print <<OUT; Javascript goes here OUT
    This allows you to print large sections of HTML / Javascript without using messy print statements.