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

I have a web page call a perl script then it changes a file and then I want to call a javascript to display the info in the file. Is there a way I can call that javascript from my perl program or return something so the web page knows when to call the javascript?
Thanks, I'm new to perl sorry if this is an obvious question.

Replies are listed 'Best First'.
Re: call javascript from perl
by marto (Cardinal) on Feb 07, 2006 at 16:37 UTC
    Hi pvilleSE,

    I notice that this is your first post (under this username at least), welcome to the Monastery.
    Let us see if I am following this:

  • You have a web page which calls a Perl script


  • How is this being called? Server side include? A form which gets submitted, or a link that is clicked?.

  • The Perl script changes the contents of a file, and you wish to output the contents of said file to the screen
  • .

    I think the way I would do this would be not to use JavaScript to display the contents of a file, but to use Perl itself. Perl is more than capable of creating dynamic content driven html, meaning you could have a Perl script display a page with the content you wish already inside it. Have a look at CGI if you have not already done so. The Tutorials section of this site has many great tutorials on this and the use of html templating systems (and many other topics), which should help you in what you are trying to do.

    Should it be that I have missed the mark on what you are trying to achieve, let us know by providing a more detailed description of what you are trying to do (break it down step by step if you have to), and any code you already have.

    Hope this helps.

    Martin
Re: call javascript from perl
by saintmike (Vicar) on Feb 07, 2006 at 17:23 UTC
Re: call javascript from perl
by olus (Curate) on Feb 07, 2006 at 17:42 UTC
    One way to do this is by invoking the perl script on an iframe, the script returning html with a javascript that dumps the info into the main html page by calling a javascript method there defined.

    In other words:
    Create the page's html with an invisible iframe like this <iframe src="your script" width="0" height="0">, and a javascript method, lets say Dumper.

    Create your script that does whatever it has to do, and then output html code with a javascript function that holds the info you want to display and passes it to the Dumper function by calling parent.Dumper(txt);

    In code:
    Main page's HTML ... <script language="javascript"> function Dumper(txt) { // write txt somewhere on the page } </script> ... <iframe src="perlscript.pl" width=0 height=0> ....
    Now the the BODY for the HTML returned by the script
    <body> <script language="javascript"> var txt='the info you want to show'; parent.Dumper(txt); </script> </body> ...
      Thank you all for your advise, I tried what olus said but I couldn't get it to execute my script command, what I ended up doing is a sort of redirect back to my original page which then calls my javascript to reload the data.

      if($ENV{'HTTP_REFERER'}=~ m/results=results/i)
      {
      print "Location:".$ENV{'HTTP_REFERER'}."\n\n";
      }
      else
      {
      print "Location:".$ENV{'HTTP_REFERER'}."?results=results\n\n";
      }

      this seems to be the easiest way to get the browser to call a javascript after you are done with a perl function. If someone wanted to use this to call a javascript function you just need to make an html page to call it and put the address where I put ENV{'HTTP_REFERER'}.