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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable text in a static web page 2

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
The index page of a web site is static.
I want to vary a couple of words of text according to the time of day the page is loaded.
I could do it with images using the 'src' element but is it possible to do the same with text returned from a Perl script without resorting to iframes etc?

Keith
 
SSI tags.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I've hardly ever come across a web server that has SSI enabled, so if you can't use SSI (google "Server Side Includes" for info on it) and you're stuck with a flat static HTML page, but you have CGI support, and you don't mind requiring JavaScript of the end users...

A) Just use JavaScript to calculate the time of day and display the dynamic text you want. There are plenty of examples online for how to do this. Or...

B) Use a JavaScript include tag that pulls its source from a Perl script.

Code:
#!/usr/bin/perl -w

use strict;
use URI::Escape;

my @localtime = localtime();
# ...do whatever...

# collect output into this variable
my $output = "some random witty quote based on time of day";

# write out some valid JS code (URI::Escape will
# ensure it's valid no matter what ya put in $output!)
$output = uri_escape($output);
print "Content-Type: text/javascript\n\n";
print "document.writeln(unescape(\"$output\"));\n";
exit(0);

Code:
<script type="text/javascript" src="/cgi-bin/time.cgi"></script>

And then "time.cgi" would write out some JavaScript code to write HTML code to the page, and we used URI::Escape so you don't need to worry about quotes or any other special symbols that might break the JavaScript code because they all get encoded, and the JS decodes them on-the-fly while writing the text to the page.

Or, if it's practical to replace your HTML page with a Perl script, just do it like this:

Code:
#!/usr/bin/perl -w

# do whatever

# print your page
print "Content-Type: text/html\n\n";
print <<EOF;
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<h1>Testing!</h1>

<span style="color: blue">This page has been displayed $hits times.</span><p>

Your IP address is $ENV{REMOTE_ADDR}

</body>
</html>
EOF

# end

It should be easy to just dump your whole HTML page into that EOF block and then variables can be interpolated just fine (use \$ if you want any literal $'s... that's true for all Perl operators here).

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
I've hardly ever come across a web server that has SSI enabled

I've never encountered a web hosting service that did not have SSI enabled by default.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Does it have to be done with Perl? Depending on where your extra data is coming from, it could be done entirely in JavaScript without requiring anything server-side at all.
 
Thanks for the assistance guys.
The whole site is dynamic except of course for the initial index page, to which I want to add the extra bits.
I thought early on that SSI was the way but I couldn't get it to work.
In a moment of inspiriation, I realised that the page required a .shtml extension, duh!
I live and learn, thanks again for pointing me in the right direction.

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top