views:

56

answers:

1

I have a page that when loaded I would like it to run a perl script. Is this possible to do with javascript? I have never ran a perl script on the web before and the only way I have seen to do it is link to it.

+7  A: 

There are 3 ways:

  • If it's a dynamic page (CGI or other), as long as your script backing the page is an executable Perl script returning valid HTTP response, you're good. If you need to execute a separate Perl script, you can do it using the usual system() call (ideally, the functionality should be a Perl library call so your script can then execute it without spawning a system call). This approach of course works with ANY language that the back-end script is written in, e.g. if it's a Java Servlet, or any other code, it can also execute a system call to your Perl script.

  • If it's a static HTML page, you can have an "onload" JavaScript event (or just a JS function call inside a <script> tag) which executes an AJAX call to a different dynamic page (say http://yourserver/scripts/run_script_X ) running the script as per previous bullet point.

    Just to be clear, the script will be running on the web server and not in a browser/client system; so you need some sort of mechanism for allowing the results of the script to affect your web page if you wish to have such an effect.

    $.ajax({url: 'http://yourserver/scripts/run_script_X'}); // jQuery example
    
  • As an oldie variation on the last approach, you can also have your page have a <IFRAME> whose URL points to http://yourserver/scripts/run_script_X (make the iframe small/invisible if you don't care about the results of running the script as per your comment).

    <!-- The rest of your HTML code -->
    <IFRAME
        SRC="http://yourserver/scripts/run_script_X"
        style="display: none;"
    />
    

Irelevant comment made prior to the comments on this answer:

Without a lot more context on what you want the page to be (CGI script, static HTML, etc...) and what you want the script to do and how you want the results of running the script to affect your page, it's hard to suggest anything more precise.

DVK
The script I will have is just going to add some stuff from a file to a database. It will not effect anything but add to the MySQL database and I really dont want the client to even know it ran
shinjuo
@shinjuo - so it'll be some sort of an invisible visit counter or somesuch, correct?
DVK
Pretty much yes
shinjuo
@shinjuo - are you using any javascript frameworks? (Prototype, YUI, jQuery)? So I know which example to include for #2
DVK
I have no framework setup right now but I have used jQuery before so I will probably use that
shinjuo
@DVK I appreciate all the help
shinjuo