Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it OK if I do following in JavaScript?

Status
Not open for further replies.

perlguy

Programmer
May 17, 2001
26
US
I would apprecaite if any JavaScript guru here correct me on the following , I have following code in my "index.html"
page:
<html>
<head>
<title>This is a test</title>
</head>
<body>
<h1> Testing whether it works or not</h1>
<table width=&quot;100%&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
<tr>
<td>

<SCRIPT SRC=&quot;mydomain/cgi-bin/banner.pl&quot; LANGUAGE=&quot;JavaScript&quot;></SCRIPT>

</td>
</tr>
</table>

</body>
</html>

Where the 'banner.pl' runs my random logic and generates code : (here is the part of code - only banner related)

$banner = &quot;document.write('<A HREF=&quot;$bannerurl&quot;><IMG SRC=&quot;$gifname&quot; width=50 height=10></A>');
which eventually results as below:
<A HREF=&quot;xx.com&quot;><IMG SRC=&quot;xx.gif&quot; width=50 height=10></A>

My only concern or doubt is Is this OK if I put Javascript inside <TABLE> .... </TABLE> the way I did it in my html page. Is it valid to do so .... or I have to print the whole <TABLE> ....</TABLE> from perl program.

please help. appreciated.

 
I think you'll want to use SSI..
Code:
<html>
<head>
<title>This is a test</title>
</head>
<body>
<h1> Testing whether it works or not</h1>
<table width=&quot;100%&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
<tr>
<td>

Code:
<SCRIPT><!--#exec cgi=&quot;cgi-bin/banner.pl&quot; --></SCRIPT>
Code:
</td>
</tr>
</table>
</body>
</html>

You'll need to modify banner.pl to print your js...
Code:
print &quot;document.write(\&quot;<a href=$bannerurl><img src=$gifname>\&quot;)&quot;;

Be sure to watch your character escaping (mind which language your escaping for!)

Hope this helps :)
 
perlguy-

lol. js masters. hah! I dont think so. ;-) (just kidding guys!)

there is no problem with inserting js inside a table tag. However, I would be interested to learn why you decided to use a perl script to write a javascript that prints on a page, instead of just using a perl script to write the page.

theEclipse
eclipse_web@hotmail.com
**\\||It was recently discovered that research causes cancer in rats||//**
 
gerry- it would appear that we posted at the same time, so I didnt get to read your post before posting myself.

perlguy- what gerry said is absolutly correct, what he did simplifies things, alot. but I would still like to know why you dont just use perl to print out the page ? theEclipse
eclipse_web@hotmail.com
**\\||It was recently discovered that research causes cancer in rats||//**
 
so what if I just put following lines in my html page
<SCRIPT SRC=&quot;mydomain/cgi-bin/banner.pl&quot; LANGUAGE=&quot;JavaScript&quot;></SCRIPT>

my banner.pl will print the whole table contents.....
Is it legal in Javascript ..... Will it work in all browser IE / NE .....

Any suggestions appreciated.
perlguy
 
This wont work. To run and 'embed' a script in a page, or to get the value returned by a program, or have any html page interact with an external file (other than a .js file), you traditionally use SSI. If banner.pl outputs a banner ad, you should use SSI (this is a common thing to do).

You would do it like this:
Code:
<!-- #exec cgi=&quot;/cgi-bin/banner.pl&quot; -->

This will call the program and place the returned html in your html page.

Am I making sense? LOL

Try looking up SSI in any search engine and this should get u started.

A note for beginners: you cannot include a script with a query string,
Code:
<!-- #exec cgi=&quot;/cgi-bin/banner.pl?showad&quot; -->
, would be an invalid call to the script. To pass a query string to a script via SSI, you would attach the querystring to the page you've called the script from. So if you had banner.pl SSI'd in index.html, and you needed to show banner.pl?showad, you would call index.html?showad. Hope I Was Able to Help!

-gerrygerry
geraldschafer@hotmail.com
 
What perlguy wants to do is just fine, as long as he provides the correct output form the Perl script. You can put a <script> tag anywhere you want in the body of an HTML page. That has been standard with both major browsers for years, and I think even the other browsers.

There is absolutely no problem outputting valid Javascript from a CGI, SSI, or any server-side activity, as long as what is output fits the bill for client-side Javascript. IOW, the procedure that generates the Javascript is of no concern to the browser.

In this case, I think there is just a slight misunderstanding about what perlguy is trying to do. He is not &quot;embedding&quot; a Perl script inside a Javascript code block. He is just &quot;replacing&quot; an external .js text file with output from hist CGI script. As I said, the browser doesn't care where it gets a Javascript SRC, just that is is parseable Javascript.

There can be a couple of hidden &quot;gotchas&quot; though, from this procedure: if you are checking for a HTTP_REFERER, you won't get one from the browser when it calls a JS source. Also, you get no output of errors to the browser, so debugging is a little difficult.
 
Oh, and you also have to make sure you are outputting the right content type header, because the browser might not be able to determine it:
Code:
text/plain
.
 
If the purpose of this scripting is to simply display the correct banner ad (based on some variables or arguments), there does not need to be javascript and the cgi script. Using SSI to generate the banner would be one way to do it, using javascript would be another way. One writing the other is not necessary (in general). -gerrygerry
geraldschafer@hotmail.com
 
this is what I have been saying all along! theEclipse
eclipse_web@hotmail.com
**\\||It was recently discovered that research causes cancer in rats||//**
 
Easy..., I am not arguing about the Best Way to do something but only making the point that there is nothing involving the http and HTML protocols that prevents that method.

Usually it's not necessary, but sometimes there is good reason to call server-side dynamic output to fill in a <script> source, because you can accomplish a couple of neat tricks that would otherwise require you to refresh the page.
 
Hi everyone,
Thanks for all your help. &quot;rycamor&quot; is in right direction, that is what I wanted (in his words):
There is absolutely no problem outputting valid Javascript from a CGI, SSI, or any server-side activity, as long as what is output fits the bill for client-side Javascript. IOW, the procedure that generates the Javascript is of no concern to the browser.

This is what I mean actually. I DO NOT want to use SSI. I should have specified in my first posting. But anyway at least now I know there is nothing wrong to do with CGI.
Thanks a lot for the help from all of you.
This tek-tips forums are simply great .... so many master brains are here .... keep it up !!

perlguy.


 
Sorry but forgot to mention one last thing i.e. somehow browser cache causing problem to execute ..... I mean I added my code in 5 different html pages. when I visit to lets say '1.html' the code executes and gives me random banner , then I go to '2.html' it gives me the same banner taht was on my '1.html' ..... and so on my other pages.

I thought it will execute everytime. Why javascript is 'cached' ? Is there any way to force and execute everytime instead of cacheing?

Appreciated.
 
This is not a Javascript problem, but simply a content header problem again. I believe you need to force a
Code:
Pragma: no-cache
header for each page. (I haven't tested this, so I don't know if this means only the CGI script or actually the containing page that calls it also).

You can output this as a raw header from the CGI script, and in the plain HTML page, you can use
Code:
<meta http-equiv=&quot;Pragma&quot; content=&quot;no-cache&quot;>
inside the <head> area of the page.
 
Hi rycamor,
I tried your way but did not worked. BUT I have found another way to do it which is working but I DO NOT know whether this is allowed/worked in all the browser (I mean older version of IE/NE) The following code is tested on IE 5.00+ and NE 4.7+ . It is working !! But some javascripts Guru might think I am doing something which not or will not work in all browser?? I appreciate any comment on following:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
var ctime = new Date();
var CT = ctime.getTime();
document.write('<SCRIPT SRC=&quot; +CT+ '&quot; LANGUAGE=&quot;JavaScript&quot;></SCRIPT>');
//-->
</SCRIPT>

did I hit jackpot by doing above? If yes then this way we can always defeat cache?
 
I don't know if the Javascript method above can always defeat cache, but it is possible to do it with the right headers. I haven't messed with HTTP headers in awhile, so I might have missed something. Read the documentation on HTTP headers, and you will probably find a way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top