Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    77
    Rep Power
    12

    How do you execute perl program with javascript! help!


    Hi,

    I am absolute newbie...i need some help...how do you execute perl with javascrtipt? like:

    <SCRIPT LANGUAGE="JavaScript" SRC="http://www.yoursite/com/cgi-bin/ads.cgi">
    </SCRIPT>

    because i want to execute this:

    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    print "<HTML><HEAD><TITLE> A Very Basic Example of an HTML page created by the CGI </TITLE></HEAD><BODY><H1>\n";
    print "A Basic Page</H1></BODY></HTML>\n";

    which will just print out anythink you want.......i have tried using the above javascript but it doesnt work...do i need to add something to the perl file so it can be executed from the javascript which i would put into a html file and then view the page?

    Please help me!!

    Thanks you very much!
  2. #2
  3. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,894
    Rep Power
    6444
    You're on the right track, but youve specified the script language as JavaScript, and it's getting fed HTML, so it won't run

    You should see an error in the bottom LHS of the screen saying error on page

    You need to change your ads.cgi to output javascript which will then be run in the browser

    Instead of
    PHP Code:
    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    print 
    "<HTML><HEAD><TITLE> A Very Basic Example of an HTML page created by the CGI </TITLE></HEAD><BODY><H1>\n";
    print 
    "A Basic Page</H1></BODY></HTML>\n"
    try this
    PHP Code:
    #!/usr/bin/perl
    print "document.write('Content-type: text/html\n\n;');\n";
    print 
    "document.write('<HTML><HEAD><TITLE> A Very Basic Example of an HTML page created by the CGI </TITLE></HEAD><BODY><H1>);\n";
    print 
    "document.write('A Basic Page</H1></BODY></HTML>');\n"
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    77
    Rep Power
    12

    doesnt work...


    Hi,

    Thanks for your reply, but it still didnt execute the cgi script...i probaly have confused you....what i need (i dont even know if javascript on html page can execute a perl sript?) is a way of getting the script to work on a html page that has the javascript code....just say for example that the cgi script had an ad code in it from an advertiser...then all i need to do is just put a javascript code on the pages i want the ad to appear on.....and i dont want to use an ad program...i have tried them and dont like them...or is there an easier way of doing this?? lilke with PHP script or something?

    Thanks heaps for your response!
  6. #4
  7. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,894
    Rep Power
    6444
    I didn't test it, and there's an error in the javascript, but the concept works, and I've used it before.

    I considered putting up some source code that still works, and saw that I have more rewriting to do.

    What basically happens is the script directive calls for the source to be generated by the perl script, and the perl script generates javascript code, which renders the html required - its obtuse, but it works
    PHP Code:
    #!/usr/bin/perl

    print "document.write('Hello World');\n"
    A simpler, tested example - the headers and such like should already be in the html page that's calling the script

    It can be a nightmare to debug - test your javascript first, and then wrap it in the document.write

    HTH
    Ax
  8. #5
  9. No Profile Picture
    Huge Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    141
    Rep Power
    13
    i'm not sure exactly what you want, but here is a line from an HTML file i have that calls a CGI script to generate random images:
    Code:
    <img src="/cgi-bin/randPic.cgi" border="0">
    and the following line is the output of the CGI script, where $web_path is the path to the images directory (from the DOCROOT) and $voila is the name of the randomly selected image:
    Code:
    print "Location: $web_path$voila\n\n";
    this same idea could be used in a dynamic banner rotation program, if that's what you are looking for...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Posts
    1,014
    Rep Power
    15
    In my view this is quite a funny way of using Perl CGI. I'm curious what your goal is.
    BUT: it IS possible...
    Your CGI-script should contain:
    PHP Code:
    #!/usr/bin/perl

    print <<"END";
    Content-typetext/javascript

    document
    .write ('<html><head><title>A Very Basic Example of an HTML page created by the CGI </title></head><body><h1>A Basic Page</h1></body></html>')
    END 
    You need to have TWO newlines following the Content-type description, the word END should be at the first position of the line (no other whitespace allowed), and the ONLY thing on that line.

    But: why on earth would you want this setup?
    If you want Perl to generate an entire page, you'd better call the CGI-script directly. In that case of course, your content-type header should be:
    PHP Code:
    print <<"END";
    Content-typetext/html 
    Again: TWO newlines.

    A nice way of using available Perl code, however, would more sense to me.
    Something like:
    PHP Code:
    #!/usr/bin/perl

    use strict;
    my $teststring 'This is content from Perl';
    my @testarray  = ('item1''item2''item3');

    print <<
    "END";
    Content-typetext/javascript

    document
    .write ('<h1>$teststring</h1>\\\\n')
    END

    for (0..$#testarray) {
        
    print <<"END";
        
    document.write ('<p>$testarray[$_]</p>\\\\n')
    END

    The newlines are double-escaped (once for Perl, once for javascript).
    You could call this with the following HTML-code:
    PHP Code:
    <html>
    <
    head>
    <
    title>A Very Basic Example of an HTML page created by the CGI</title>
    </
    head>
    <
    body>
    <
    script type="text/javascript" src="http://www.yoursite/com/cgi-bin/ads.cgi"></script>
    </body>
    </html> 
    Hope this helps,
    Jeroen
    Last edited by jerom; May 4th, 2003 at 12:04 PM.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    77
    Rep Power
    12

    it works!!


    It works....thankyou so much Jarom and everyone else for there help...the reason i wanted to do it was because i am new to perl and javascript i was going to jsut write a very simple program to use.....i was going to add the codes for banners etc...jsut a very simple banner program......all i need to do is put the javacode on all the html pages, then just add the ad codes i want dispplayed on the pages to the cgi file....and i know it sounds stupid because i could just use a banner manager for free like PhP ads new etc....but im just experimeting for now....once again thanks heaps Jarom and everyone else Keep up the great work!!!

    And one quick last one....is it possible to add javascript to the cgi file...so that it will be printed on the html file...and thus displaying a the banner??

    Thanks
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    77
    Rep Power
    12
    more info:

    Just say i wanted to add the following code to the cgi script:


    <SCRIPT LANGUAGE=JAVASCRIPT>
    <!--
    var account = 'username';
    var category = '11';
    var pop = 'up';
    var entry = 'entry';
    //-->
    </SCRIPT>
    <SCRIPT LANGUAGE=JAVASCRIPT SRC="http://server01.popupmoney.com/popup.js?username&11&up&entry"></SCRIPT>

    Could i get that to be displayed in the html when the <script type="text/javascript" src="http://www.yoursite/com/cgi-bin/ads.cgi"></script> executes the cgi script on the page..that way the popup or banner will be displayed (as the code above)

    How would i do all this?

    I tried playing the banner code in the use strict;
    my $teststring = 'This is content from Perl';
    my @testarray = ('item1', 'item2', 'item3'); but it didnt work...the script works fine to just print out what it says...but not the banner?

    Thanks
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Posts
    1,014
    Rep Power
    15
    I don't think you can call a javascript include from within a text/javascript content-type.
    One more hint:
    Code:
    print <<'END';
    Content-type: text/html
    
    document.write ('<script type="text/javascript">')
    document.write ('alert ("I\\'m generated!")')
    document.write ('</script>')
    END
    I'm not going any further, because I HATE popups, and I told my favorite browsers to block all automatic popup window from showing up...

    Jeroen
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    77
    Rep Power
    12

    thanks...


    yeah i know i hate popups too....i only used the popup code as an example of the kind of javascripts to try and print......
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    77
    Rep Power
    12
    Jerom its not for popups, i swear....you dont realise how much i hate them as well.....i only used that javascript as an example, because as you would know i am trying to learn how to create my own advert script....so can u please help me....you have been so so helpfull so far! How would this be printed (its not a popup code!) :

    <!--Begin RmbClick Advertisng Network Ad Code -->
    <SCRIPT LANGUAGE="JavaScript" SRC="http://m.rmbclick.com/cgi-bin/cpm/cpmad.cgi?uid=yoursitesname&cid=gaming">
    </SCRIPT>
    <!--End RmbClick Advertisng Network Ad Code -->

    Please, please help me!! help the newbie cgi folks!!

    help me my friend??
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Posts
    1,014
    Rep Power
    15
    In my last post I gave an example how you could make the construction...
    If you read it carefully, the solution is really simple.

    This is what you seem to need.
    In your cgi:
    Code:
    print <<'END';
    Content-type: text/html
    
    document.write ('<!--Begin RmbClick Advertisng Network Ad Code -->')
    document.write ('<script type="text/javascript" src="http://m.rmbclick.com/cgi-bin/cpm/cpmad.cgi?uid=yoursitesname&cid=gaming">')
    document.write ('</script>')
    document.write ('<!--End RmbClick Advertisng Network Ad Code -->')
    
    END
    In your HTML page:
    Code:
    <script type="text/javascript" src="pathtoyour.cgi"></script>
    But -again- why don't you just simply include the advertising script directly into your HTML pages (, instead of making a detour into a Perl cgi script)?
    Is this really what you want?

    Hope this helps,
    Jeroen
  24. #13
  25. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    77
    Rep Power
    12
    thank you so much cheif!!! the reason i want to have the cgi script is so that all i need to do is put the javascript code to execute the cgi script on the pages..and therefore i only have to put the code on the html pages once, and then just change the code in the cgi script when i need to change the advert code...that way i dont have to change the ad code on the html pages every time i want to change the advertiser....do you see what i mean? And also how do i learn to write cgi like you can?? do i need to go to Uni, or will some books just be enough??

    Once again thankyou so much, you are a legend in this forum..!!!


    thanks heaps
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    77
    Rep Power
    12
    it doesnt seem to work...it doesnt bring up the banner....just a blank page...any ideas to why this would happen??

    thanks...
  28. #15
  29. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,894
    Rep Power
    6444
    If there's a syntax error in your javascript it won't work

    You should see the 'warning' icon in the bottom left on the status bar, if there's an error in the javascript.

    Test your javascript in isolation before wrapping it. It can be a nightmare to debug, but well worth it

    HTH
    Ax
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo