Assigning a CGI output to JavaScript variable.

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

    Assigning a CGI output to JavaScript variable.

    How can I assign a return value of a CGI to a JavaScript variable? Or
    what is the correct way?
    <script>
    var myData = eval(http://somewhere/cgi-bin/data.cgi");
    </script>

    TIA

    Sam
  • Martin Honnen

    #2
    Re: Assigning a CGI output to JavaScript variable.



    sams wrote:
    [color=blue]
    > How can I assign a return value of a CGI to a JavaScript variable? Or
    > what is the correct way?
    > <script>
    > var myData = eval(http://somewhere/cgi-bin/data.cgi");
    > </script>[/color]

    You could use
    <script type="text/javascript" src="/cgi-bin/whatever.cgi"></script>
    and then let that CGI program generate
    var myDate = "...data goes here...";
    If you want to read in the output of someone's CGI program then in
    general that is not doable as the same origin policy doesn't allow
    access to another server.
    If you have need to read in any page from your own server then some
    browsers like IE5+/Win or Mozilla/Netscape 6/7 allow you to send a HTTP
    request and receive the response including the response text. It doesn't
    matter if you request a static HTML page or a CGI. For instance with
    Mozilla you can do

    var httpRequest = new XMLHttpRequest( );
    httpRequest.ope n('GET', 'whatever.cgi', false);
    httpRequest.sen d(null);
    alert(httpReque st.responseText )

    Note that the above requests the resource synchronously, meaning the
    script waits for the response and the browser is blocked while doing the
    request so you should do it asynchronously if you want the browser to
    stay responsive:

    var httpRequest = new XMLHttpRequest( );
    httpRequest.ope n('GET', 'phpinfo.php', true);
    httpRequest.onr eadystatechange = function (evt) {
    if (httpRequest.re adyState == 4) {
    alert(httpReque st.responseText );
    }
    };
    httpRequest.sen d(null);


    --

    Martin Honnen


    Comment

    Working...