Passing CGI output string into Javascript variable.

Discussion in 'Javascript' started by stahl.karl@gmail.com, Jan 17, 2006.

  1. Guest

    I have a CGI/Perl program that returns a string output. Is it possible
    to get this into a Javascript variable, where the name of the variable
    is defined in the Javascript and not in the Perl code? For example, it
    seems that the following should work, but it doesn't appear to. Is
    this valid code?


    CGI code --- get_string.pl ------

    print "Content-type: text/plain\n\n";
    print "ahoy matey";
    -------------------------------------------


    Javascript/html ------------------

    <script type="text/javascript">
    var mystring = eval('http://blabla/cgi-bin/get_string.pl');
    document.write(mystring);


    Thanks for your help!
     
    , Jan 17, 2006
    #1
    1. Advertising

  2. RobG Guest

    wrote:
    > I have a CGI/Perl program that returns a string output. Is it possible
    > to get this into a Javascript variable, where the name of the variable
    > is defined in the Javascript and not in the Perl code? For example, it
    > seems that the following should work, but it doesn't appear to. Is
    > this valid code?


    You could just write it to the page in a script element, but I guess you
    don't want to do that.


    > CGI code --- get_string.pl ------


    Make that say: get_string.js


    > print "Content-type: text/plain\n\n";


    Serve it as the appropriate type for JavaScript.


    > print "ahoy matey";


    Write the following to get_string.js:

    var mystring = 'ahoy matey';


    > -------------------------------------------
    >
    >
    > Javascript/html ------------------
    >


    Make sure this script element is placed before the one below:

    <script type="text/javascript" src="get_string.js"></script>


    > <script type="text/javascript">
    > var mystring = eval('http://blabla/cgi-bin/get_string.pl');


    Eeek, delete that. eval will try to execute the string as if it were
    script - URLs aren't script so the results will not be what you are
    expecting.


    > document.write(mystring);


    mystring is defined in get_string.js which has been loaded by the
    previous script element and is therefore available for use - the
    document.write() statement will write 'ahoy matey' to the page.

    If it's in the body, you should see the result.


    [...]


    --
    Rob
     
    RobG, Jan 17, 2006
    #2
    1. Advertising

  3. Guest

    Thanks for your help. I guess I'm being finicky, but is there no way
    of simply returning the string from the CGI script? I'd rather define
    the variable name in the Javascript part - that way if I want to change
    the variable name, I can just change the Javascript and leave the CGI
    untouched. It just seems so inelegant to use language A to write out
    code word-for-word in language B.
     
    , Jan 17, 2006
    #3
  4. RobG Guest

    wrote:
    > Thanks for your help. I guess I'm being finicky, but is there no way
    > of simply returning the string from the CGI script? I'd rather define
    > the variable name in the Javascript part - that way if I want to change
    > the variable name, I can just change the Javascript and leave the CGI
    > untouched. It just seems so inelegant to use language A to write out
    > code word-for-word in language B.



    The basis for client-server communication on the web is HTTP, which is
    essentially the exchange of text instructions. Those instructions may
    include links to binary files or streams, but browsers do not interpret
    those - they are either displayed (e.g. GIF, JPEG, PNG images), handed
    to a plugin (e.g. PDF, Flash, MPEG) or given to a helper application
    (e.g. FlashGet for downloading large files).

    The only web-safe way to send instructions to the browser is to put them
    into a text file (HTML, script, XML, etc.). If that doesn't suit, use
    some other protocol and a plugin or helper application.


    --
    Rob
     
    RobG, Jan 17, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonas Daunoravicius
    Replies:
    5
    Views:
    325
    Peter
    Jul 17, 2003
  2. Professor Chen
    Replies:
    0
    Views:
    230
    Professor Chen
    Feb 6, 2006
  3. Jonas Daunoravicius
    Replies:
    5
    Views:
    295
    Peter
    Jul 17, 2003
  4. sams
    Replies:
    1
    Views:
    207
    Martin Honnen
    Oct 12, 2003
  5. michael
    Replies:
    3
    Views:
    342
Loading...

Share This Page