calling Perl script from javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Trahan

    calling Perl script from javascript

    I want a js function to call a Perl script residing on a server.
    The Perl script will return a string, to be used by the js.
    Pseudo code:
    <script>
    stringvar = perlfunc_on_ser ver(stringarg)
    document.write( stringvar)
    </script>

    Can stringvar above be a url?

    The perlfunc_on_ser ver could take the form of a GET,
    like https://www.myserver.com/cgi-bin/per...?arg=stringarg

    Any other kludge is okay, as long as it works.

    I'm very experienced with Perl, but a newbie to js.
    Source code would help. Thanks.

  • Erwin Moller

    #2
    Re: calling Perl script from javascript

    Richard Trahan wrote:
    [color=blue]
    > I want a js function to call a Perl script residing on a server.
    > The Perl script will return a string, to be used by the js.
    > Pseudo code:
    > <script>
    > stringvar = perlfunc_on_ser ver(stringarg)
    > document.write( stringvar)
    > </script>
    >
    > Can stringvar above be a url?
    >
    > The perlfunc_on_ser ver could take the form of a GET,
    > like https://www.myserver.com/cgi-bin/per...?arg=stringarg
    >
    > Any other kludge is okay, as long as it works.
    >
    > I'm very experienced with Perl, but a newbie to js.
    > Source code would help. Thanks.[/color]

    Hi,

    If you need JS to get some interaction with a SERVERSIDE script, you have
    to:
    1) call that script
    2) receive the response and do something usefull with it.

    the easiest way is to use a hidden frame (or a new window, as long as you
    have a window) in which you replace the URL with your call to the
    Perlscript.
    The Perlscript has to respond in a way that can be used in javascript.
    In such situations I always let the server respond with a normal HTML-page
    that has an onLoad-handler that call some javascript-function.
    That function contains serverside information you need.

    Eg (php example because my Perl sucks)

    <html>
    <head>
    <script type="text/javascript">
    function passInfo(){
    alert("I received: <?= $_GET["userid"] ?>");
    }
    </script>
    </head>
    <body onLoad="passInf o();">
    Leave this empty, not needed.
    </body>
    </html>

    if you call the above page in some hidden frame it will respond with an
    alert saying 'I received: 23':
    example.php?use rid=23

    The $_GET example is lame of course, but you can just do all your serverside
    Perlstuff over there.
    Instead of the alert you can call some JS-function in your main-page,
    possibly passing the needed info from the server.

    Stuff you need to know to do this is:
    1) how you make (hidden) frames. (frame and frameset)
    2) replace the content of a window
    top.frames["framename"].location = .......
    3) how to make calls to a JS-function in another window.
    top.frames["someFrameN ame"].someFunction(1 ,2,3);

    Hope that helps.
    Good luck,
    Erwin Moller

    Comment

    • Martin Honnen

      #3
      Re: calling Perl script from javascript



      Richard Trahan wrote:
      [color=blue]
      > I want a js function to call a Perl script residing on a server.
      > The Perl script will return a string, to be used by the js.
      > Pseudo code:
      > <script>
      > stringvar = perlfunc_on_ser ver(stringarg)
      > document.write( stringvar)
      > </script>[/color]

      Well if you want to receive some text from the Perl script and display
      it directly (document.write does that) I am not sure why you need
      JavaScript at all, you could solve that on the server and use Perl to
      read the text from the (other?) server and then include that in your
      page with Perl.

      [color=blue]
      > Can stringvar above be a url?[/color]

      What you can do with HTML is
      <script type="text/javascript"
      src="www.myserv er.com/cgi-bin/perlfunc_on_ser ver.pl?arg=stri ngarg"></script>
      that way you simply need to make sure your perlfunc_on_ser ver.pl sets
      the Content-Type to application/x-javascript and sends syntactically
      correct JavaScript back.
      [color=blue]
      > The perlfunc_on_ser ver could take the form of a GET,
      > like https://www.myserver.com/cgi-bin/per...?arg=stringarg
      >
      > Any other kludge is okay, as long as it works.
      >
      > I'm very experienced with Perl, but a newbie to js.
      > Source code would help.[/color]

      The biggest problem could be the same origin policy governing
      client-side scripting so while in theory in new browsers like Mozilla,
      IE5+/Win, Safari you can make HTTP requests and receive the result you
      can usually only connect back to the server the HTML page with the
      script comes from.
      See



      There are also solutions using an iframe (usually a hidden one) to
      perform the request to the server and returning some script that updates
      the document containing the iframe but in this case too you are
      restricted by the same origin policy.


      --

      Martin Honnen


      Comment

      • Richard Trahan

        #4
        Re: calling Perl script from javascript

        Thank you for your detailed reply.

        Although a great idea, I cannot create an invisible frame
        because the client window is an eBay ad. eBay software will
        detect the frame creation code and delete the ad, as they
        explicitly prohibit frames.

        Is it true in general that js functions which take a string
        argument will also take a url? I.e., can I do something like
        alert(url(http://myserver.com)) ?

        Comment

        • Andre Herbst

          #5
          Re: calling Perl script from javascript

          Richard Trahan wrote:
          [color=blue]
          > Is it true in general that js functions which take a string
          > argument will also take a url? I.e., can I do something like
          > alert(url(http://myserver.com)) ?[/color]

          This is partly true. Because an URL is also a string. You could call the
          function:

          alert("http://myserver.com")


          Comment

          • Richard Trahan

            #6
            Re: calling Perl script from javascript

            Martin Honnen wrote:

            Thank you for responding.
            [color=blue]
            > What you can do with HTML is
            > <script type="text/javascript"
            > src="www.myserv er.com/cgi-bin/perlfunc_on_ser ver.pl?arg=stri ngarg"></script>
            >
            > that way you simply need to make sure your perlfunc_on_ser ver.pl sets
            > the Content-Type to application/x-javascript and sends syntactically
            > correct JavaScript back.[/color]

            That's exactly what I'm looking for. Thank you so much. I have not
            been able to find a decent textbook or tutorial that explains exactly
            how the various MIMEs are used in CGI programming.
            The texts on network programming are all obsessed with
            sockets, not CGI. Do you know of a good book?


            Comment

            • Robert

              #7
              Re: calling Perl script from javascript

              In article <cf5quv$aln$1@n gspool-d02.news.aol.co m>,
              "Andre Herbst" <moorkonig@comp userve.de> wrote:
              [color=blue]
              > alert("http://myserver.com")[/color]

              Of course, it is going to display a message box with:



              Probably not what the OP wants, but not sure what the OP is hinting at.

              Robert

              Comment

              Working...