View Full Version : can a cgi supply js code for <script src= ??

04-03-2003, 07:01 PM
Thanks for taking the time to read this, because if you're good at JavaScript, I can really use your help.

Within a browser that supports scripts, the following line should fetch a javascript program, run it, and even allow it to affect the content of the current page (document.write(), etc.):
<script language=JavaScript src="http://x.x.x.x/my_prog.js"></script>

Is it possible to let a good ol' C/C++ program (or any other language, for that matter), feed back the JavaScript program on stdout? Consider the following:
<script language=JavaScript src="http://x.x.x.x/cgi-bin/feed_js_code_to_browser.cgi"></script>

What I know so far:
The httpd on my server will actually respond to this by running the requested program. Anything written to stdout actually makes it back to the browser, but it is not interpreted as a javascript program. When just sending Javascript lines of code, I get errors. As a test, I sent back html, and the browser removes the current content and replaces it with what I sent.
I think the hurdle involves properly formatting the JavaScript lines of code fed back to the browser. Quoting the O'Reilly koala book for HTML, in regards to the src= attribute:
"The value of the src attribute is the URL of the file containing the JavaScript program. The stored file should have a MIME type of application/x-javascript, but it will be handled automatically by a properly configured server if the filename suffix is .js."
This leads me to believe that this is possible, if I can just properly format the text of the JavaScript code lines sent to the browser.

Thanks in advance for any hints or leads you can give me.


04-03-2003, 08:51 PM
You've answered your own question - yes it works, but yes you have to be very careful with the syntax - usually because server side languages generate error messages in HTML, which are written unescaped into javascript variables, and so generate javascript errors.

It's much easier if you print the javsacript you want to generate into the main HTML source code while you're developing, then turn it into an include when you've finished.

It's a bit OT, but I wrote a tutorial about PHP-generating javascript includes for my dropdown menu; it might help - http://www.brothercake.com/dropdown/dynamic.html#php

04-04-2003, 09:51 PM
Thanks for trying, brothercake. I went and read your write-up, but got lost as there were many different language examples. Also, I am not completely sure that include files is the same idea I am trying to accomplish. Can we break it down to the smallest possible example?

html file has the following line:
<script language=JavaScript src="http://x.x.x.x/cgi-bin/send_back_js_code"></script>

send_back_js_code is a C program, capable of writing to stdout. send_back_js_code wants to write a 1 line, valid, JavaScript program. How would I have to package the following to get the browser to consider it a JavaScript program and run it?

alert("I have been successfully loaded and executed.");

I have tried several things, like outputting a line with "application/x-javascript" first, before the javascript code line.

Thanks again for any help you can offer


04-04-2003, 09:56 PM
Well I don't know C, but I can give you a simple example in PHP:

$color = "red";
echo ("alert('" . $color . "');");

So there I define a $color variable, and write into a javascript alert - I'm creating that javascript as though it were just writing a string, which as far as PHP is concerned, it is; but by the time it gets to the client, it looks like this:


With that PHP code in a file called "myscript.php" you can just include it like normal javascript:

<script type="text/javascript" src="myscript.php"></script>

The only real caveat is that if you compile a javascript include on the server like this, the file won't have access to the document environment variables - so you can't make its data conditional on things like the document URL or referrer; however it will have access to server environment variables - session information, other data sources, and so on.

Yes? :)