Running javascript from within cgi-Script
June 15, 2005 8:18 PM   Subscribe

I use a free cgi mailing list script that generates a html template for the admin interface and user feedback for sign-up. The site i am implementing it on today has some basic preload/rollover javascript within the HEAD of the HTML document, but the javascript aint working on the template the cgi outputs. How do i need to format the javascript so that it works from the cgi-script?

Is it the paranthese, curlies, semicolons or brackets that need to be slashed? Or all four? Here's what I got:
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i
posted by elphTeq to Computers & Internet (3 answers total)
 
...eeek, it was all there on preview, promise!

anyway, i've slashed all my quoted values, what else do i need to slash or change... ?
posted by elphTeq at 8:23 PM on June 15, 2005


Examine the output of your cgi script. That output should be valid HTML, and the included JavaScript should be valid JavaScript.

As far as "slashing" -- the actual term is "escaping", that depends on the language your cgi script is written in. For most procedural languages that derive from the C language, you'll need to escape lines feeds, carriage returns, double quotes, tabs, and forward slashes in literal strings.

Note that as these entities are also escaped in JavaScript, you'll need to take special care of literal strings within the generated JavaScript.

Examples: In C derived languages, including JavaScript, this represents a singe character, the "newline": "\n" To represent a single slash, we use two slashes, because otherwise the slash introduces an escape sequence: "\\".

In JavaScript, to show a two line alert, you'd write this (note the newline):
alert( "this is line one\nthis is line two" );
But if you wanted to output a newline in your cgi-generated JavaScript, you'd need to escape the double quotes but also the forward slash, so the C code would actually output a forward slash followed by a regular "n", which the JavaScript would interpret as a forward slash followed by an "n", which is the escaped code for a newline in JavaScript. So the C code to generate the JavaScript code too produce the same two line alert would be (with the additional slashed in boldface):
printf( "alert( \"this is line one\\nthis is line two\" );" ) ;
A far simpler way to achieve what you want would be to simply include a reference to the javscript in the cgi-output HTML:
<script language="text/JavaScript" src="another.file.of.pure.javascript.js">

posted by orthogonality at 8:55 PM on June 15, 2005


Cheers orthogonality, i've come to the same conclusion, unfortunately I just had to take the mofo scenic route via the night train to get there :)

All i needed was the keyword "escaping" to get anywhere on google - thanks!
posted by elphTeq at 9:09 PM on June 15, 2005


« Older extreme gender expression   |   summer reading Newer »
This thread is closed to new comments.