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

Hi Monks,
I want to invoke a javascript function just like calling perl function. Is that possible?
Something like this
my $js=<<SCRIPT; function update(msg){ obj=getElementByID('box'); val=obj.value(); alert(val); } SCRIPT ...... for (my $i=0;$i<3;$i++){ update(msg);#call JS function }
Thnkx

Replies are listed 'Best First'.
Re: Invoking Jscript function without actionevent
by moritz (Cardinal) on Apr 18, 2008 at 09:59 UTC
    You're mixing Perl with Javascript, and most likely client with server side. Both of that isn't that easy.

    You should never have to execute javascript on the server, instead you have to send HTML code to the client that invokes the javascript.

Re: Invoking Jscript function without actionevent
by elmex (Friar) on Apr 18, 2008 at 11:23 UTC

    As said in the comments above: You are confusing the languagesand client/server side.

    I assume that you are doing CGI and usually have the Javascript code transmitted to the webbrowser.

    Javascript is usually executed in the browser and only printed out as a text string by the Perl script.

    Your scalar $js contains a text string which contains characters which form syntactically correct javascript. Perl can't execute or parse that code (you would need a javascript interpreter for that in Perl, which probably can even be found on CPAN, but this has nothing to do with what you want to do).

    If your CGI script needs to transmit something to the javascript code running in the browser you should look into AJAX (google it!), which is basically: The browser sends another request to your CGI script, which will return information that will be processed by the Javascript code in the browser to update the html in some way.

Re: Invoking Jscript function without actionevent
by Anonymous Monk on Apr 18, 2008 at 09:57 UTC
    No.