Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-03-2003, 06:01 PM   PM User | #1
TAS
New to the CF scene

 
Join Date: Apr 2003
Location: Florida
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
TAS is an unknown quantity at this point
can a cgi supply js code for <script src= ??

Folks,
Thanks for taking the time to read this, because if you're good at JavaScript, I can really use your help.

Background:
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>

Question:
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.

-TAS
TAS is offline   Reply With Quote
Old 04-03-2003, 07:51 PM   PM User | #2
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
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
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

Last edited by brothercake; 04-03-2003 at 07:53 PM..
brothercake is offline   Reply With Quote
Old 04-04-2003, 08:51 PM   PM User | #3
TAS
New to the CF scene

 
Join Date: Apr 2003
Location: Florida
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
TAS is an unknown quantity at this point
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

-TAS
TAS is offline   Reply With Quote
Old 04-04-2003, 08:56 PM   PM User | #4
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
Well I don't know C, but I can give you a simple example in PHP:
PHP Code:
<?
$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:
Code:
alert('red');
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?
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

Last edited by brothercake; 04-04-2003 at 09:08 PM..
brothercake is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:41 AM.


Advertisement
Log in to turn off these ads.