cgi scripts and external css

P

Peter Seidel

Consider this CGI script:

#!ruby
require "cgi"
cgi = CGI.new("html4")
cgi.out {
CGI.pretty(
cgi.html {
cgi.head { cgi.title{"Example"}+"<link rel=\"stylesheet\"
type=\"text/css\" href=\"example.css\">" } +
cgi.body{"This should have a green background"}
}
)
}

with this example.css, both residing in the same directory:

body {
background-color: #00ff00;
}

it produces this html output:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>
Example
</TITLE>
<link rel="stylesheet" type="text/css" href="example.css">
</HEAD>
<BODY>
This should have a green background
</BODY>
</HTML>

Perfectly fine, it seems. The only problem is, that the background isn't
green, it's white (Firefox' default color, on my system).
However, if I save the html-source to a static example.html, put in the
same directory as my example.rb and open it in the browser, the css is
correctly applied and the background turns green.

It works if I put the css directly in the example.rb like this:

#!ruby
require "cgi"
cgi = CGI.new("html4")
cgi.out {
CGI.pretty(
cgi.html {
cgi.head {cgi.title{"Example"}+
cgi.style('type'=>'text/css'){File.read("./example.css")}} +

cgi.body{"This should have a green background"}
}
)
}

But this way the css is not cacheable...

Where lies the problem in my script?

(FYI I use ruby 1.8.2 (2004-12-25) [i386-mswin32], Apache and Windows XP
Pro)
 
P

Peter Seidel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Sorry, my language was ambiguous. With "both files" I meant the script
and the css file. In fact, all the files are in /cgi-bin/.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFBokk11V8mqIQMRsRA7hPAJ0bHbk3WyiS0IMIW0iuCI9TsEMSogCffYp7
anjXtV36eFNifY0ddRH1yfo=
=grMT
-----END PGP SIGNATURE-----
 
W

William Crawford

Peter said:
Sorry, my language was ambiguous. With "both files" I meant the script
and the css file. In fact, all the files are in /cgi-bin/.

I just put example.rbx and example.css in ~/ruby on my server and it
works. The page has a green background.

I did have a problem with browser cache, though... I had tried it
before example.css existed, and it was a white background. Making
example.css and refreshing several times didn't fix the problem. I had
to tell it to do a full page reload (ctrl-click reload) before it showed
the green background.
 
M

Mike Dvorkin

When in doubt about browser caching you might want to add version
parameter to the URL, ie:

cgi.head { '<link rel="stylesheet" type="text/css"
href="example.css?v=' + Time.now.to_i.to_s + '">' } ... etc.

This works for external JavaScript files as well.

Mike Dvorkin
http://www.rubywizards.com
 
P

Peter Seidel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

I've found the problem. It seems that Apache does not serve the file
correctly if it's located in the /cgi-bin/ directory. If I move it to
/htdocs and adapt the script it works fine.
Thank you for your kind replies.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFBxe/11V8mqIQMRsRA0okAJ9K9EJHa2coYEXeSqG4ybKDQ3GOYgCeL0TJ
P09WYuLRmYjE0wfEZrLXi5Q=
=Jufx
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top