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

Is there a simple way to execute a javascript function from within a cgi page?

I've looked at javscript::spidermonkey but it seems like overkill.

Replies are listed 'Best First'.
Re: call js function from CGI?
by ikegami (Patriarch) on Apr 08, 2008 at 14:32 UTC

    Just like a static HTML page can contain JavaScript, so can a dynamically built one.

    use strict; use warnings; use CGI qw( :standard ); print( header(), start_html('A Simple Example'), qq{<p>Hey look, some HTML!</p>}, qq{<script>alert("Hey look, some JS!");</script>}, end_html(), );
      I tried this:
      print '<script>reload_foobar function();</script>';
      but no luck

        The code works for me in Perl:

        >perl -le "print '<script>reload_foobar function();</script>';" <script>reload_foobar function();</script>

        but this doesn't look very much like JavaScript to me:

        reload_foobar function();

        Maybe you can tell us what you want to do. Maybe you can also describe how Javascript::SpiderMonkey is overkill and how what you want can be done with less than pulling in a complete Javascript interpreter.

        Check your browser's error console. I'm sure there's an error message waiting for you there, since what you posted isn't JavaScript.
Re: call js function from CGI?
by leocharre (Priest) on Apr 08, 2008 at 16:11 UTC
    There is no such thing as a cgi page. Doh- you say.
    The clarity of this concept is vital.
    You have to separate the idea of the client and what it/they experiences (the audience computer/visitors) and what your application (cgi/server) is doing.
    Part of the Javascript idea is that you potentially let the end user's machine do some of the work, flip little pictures, throw javascript alerts.. draw little smily faces all over some dom node.

    Think of your cgi's output as if you had written it yourself by hand.
    So, restate your question. The right question is- Is there a simple way to execute a javascript function within a html(xhtml/rss/flash/anything downloaded from your server) page?
    This has nothing to do with perl, and everything to do with html/javascript, google for body onload/etc if you want this to happen automatically without user interaction.

    That said, CGI has some shortcuts to the html you spit out, and some minor javascript support.

    That said, my advice is- if you are coding server side, forget javascript. Serve static content in the most boring manner possible. If you use actual content in your website, users will come.
    Let the web designers and worry about javascript.

Re: call js function from CGI?
by dragonchild (Archbishop) on Apr 08, 2008 at 14:46 UTC
    Where do you want the Javascript to execute? Do you want it to execute on the server or in the browser?

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: call js function from CGI?
by neo1491 (Beadle) on Apr 08, 2008 at 18:42 UTC
    you mean something like this?
    print "<HTML>"; print "<HEAD>\n"; my $m_javaScriptPath = $m_filePath; $m_javaScriptPath =~ s/T\:Equipment Allocation\/Java//; print "<SCRIPT language=\"JavaScript\" src=\"T:/Equipment Allocation/h +tdocs/cgi-bin/java.js\"></SCRIPT>\n"; print "<LINK rel=\"stylesheet\" href=\"T:/Equipment Allocation/htdocs/ +cgi-bin/styles.css\" type=\"text/css\"></LINK>\n"; print "<TITLE>$m_project EquipmentAllocation</TITLE>\n"; print "</HEAD>\n"; print "<BODY onload=\"showError('$m_errorString')\">";
Re: call js function from CGI?
by LesleyB (Friar) on Apr 09, 2008 at 10:01 UTC

    If you don't want to write javascript but want your javascript to hook into a perl function in an asynchronous way then take a look at CGI::Ajax.

    However, if you simply want to include some javascript in your output then include the javascript by the standard HTML-based methods and the event triggers in your page output in the ways others have suggested here.