Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Javascript and Perl... a match never made.

by Anonymous Monk
on Jul 19, 2002 at 18:19 UTC ( #183354=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am looking to have a text/javascript stream created by a perl script and pushed to a client like this:

on the server side, I would have "jserver.cgi" which would accept some GET arguments...

and on the recieving end, an HTML page like this:

<SCRIPT SRC="./cgi-bin/jserver.cgi?script='somescript'" TYPE="text/jav +ascript" LANGUAGE="JavaScript 1.3"> </SCRIPT>
So this ^should^, in theory, work. But it doesn't. All I am doing in the script is:
print "Content-type:text/javascript\n\n\n"; print <<TILL_END; // JAVASCRIPT! YAY! TILL_END

So - if I have this html page and this script, what am I doing wrong? Is there something explicitly wrong with CGIing the JavaScript? I don't see why that shoudl be a problem.

Thanks, Dave

Edit by dws for format cleanup

Comment on Javascript and Perl... a match never made.
Select or Download Code
Re: Javascript and Perl... a match never made.
by kidd (Curate) on Jul 19, 2002 at 19:02 UTC
    Why dont you try using a SSI to call thw CGI...

    <!--#exec cgi="./cgi-bin/jserver.cgi?scrip=somescript"-->

    or

    <!--#include virtual="./cgi-bin/jserver.cgi?scrip=somescript"-->

    Hope it helps...

Re: Javascript and Perl... a match never made.
by gryphon (Abbot) on Jul 19, 2002 at 19:53 UTC

    Greetings anonymous,

    Several Web browsers are setup to expect that all JavaScript files included via the <SCRIPT SRC=""> tag have the ".js" extention. I ran into this problem about a year ago when I was trying to do something fairly similar. I just changed my file extention of the CGI from ".cgi" to ".js", and things worked perfectly. Of course, you may have to mess around with an .htaccess file somewhere to get the desired result.

    Dealing with the GET CGI parameters is a little tricky. You could try putting them into the URL. For example, try:

    <SCRIPT SRC="./cgi-bin/jserver.cgi/script=somescript/script.js" TYPE="text/javascript"></SCRIPT>

    In this case, there's an enviornment variable created (usually) called "PATH_INFO". It'll be equal to "/script=somescript/file.js" in this case. So now just regex the "somescript" out of that and you're done.

    my $script_name = $1 if ($ENV{PATH_INFO} =~ m|/script=([^/]+)/|);

    -gryphon
    code('Perl') || die;

Re: Javascript and Perl... a match never made.
by dree (Monsignor) on Jul 20, 2002 at 08:39 UTC
    Hello!

    gryphon says right but you can try also this old CGI trick:

    put a string after the real script name to cheat the server. In this way the name of the script will be the string.

    For example: http://mysite.com/cgi-bin/jserver.cgi/myjs.js?some_GET_stuff_here=...

    So for the browser, the name of the script will be myjs.js

    Also, pay attention with the header: you wrote 3 \n, 2 are enough, and you need a space after the ":" .You can write text/plain:
    print "Content-type: text/plain\n\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://183354]
Approved by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-04-21 09:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls