CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Other Programming Languages: Javascript:
Random cell background image

 



Melvosh
User

Sep 24, 2002, 4:38 AM

Post #1 of 4 (27600 views)
Random cell background image Can't Post

I'm trying to randomly select a background image for a table cell. I've got the array set up, and I can select a random element out of it. My question is, what's the best way to write the css to include the randomly selected image? Can you do a js document.write within <style> tags? Should I just document.write the entire table cell line, and write out the style within the table cell? Any advice would be greatly appreciated. Thanks.

P.S. I already had this working in perl, but since most of my pages on my site aren't perl, I need to be able to do it in js. Unless someone can convince our IT group to enable XSSI.
"...there's some good in this world, Mr. Frodo, and it's worth fighting for." - Samwise Gamgee, The Two Towers


fashimpaur
User

Sep 26, 2002, 8:58 AM

Post #2 of 4 (27596 views)
Re: [Melvosh] Random cell background image [In reply to] Can't Post

Melvosh,

A document.write between style tags is probably not the best way to do this.

I will write a quick example script to show you a better way and you can make
whatever decisions you want as to the direction you want to go.

Will post again soon,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


fashimpaur
User

Sep 26, 2002, 1:06 PM

Post #3 of 4 (27594 views)
Re: [Melvosh] Random cell background image [In reply to] Can't Post

Melvosh,

I am not having as easy of a time as I thought. I will have to get back to you again on this.
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


Melvosh
User

Sep 27, 2002, 9:05 AM

Post #4 of 4 (27590 views)
Re: [fashimpaur] Random cell background image [In reply to] Can't Post

fash,

It's probably not the most elegant solution, but it works for my purposes. It totally screws up in Netscape 4.7, but it did that before, since Netscape likes to tile td background images. And usage of Netscape around here is declining, and is almost nil in our department. (This is not intended to start a flame war on browsers, it's just my opinion.) Anyway, here's the code:


Code
 <script type="text/javascript"> 
<!--
var bg_Array = ["cracked_varnish", "glass", "grad1", "palette_knife", "radial_sweep", "ripple", "spatter", "sponge", "sprayed_strokes", "vortex"];
function rand(max) {
return Math.round(Math.random() * max);
}


document.write("<style type='text/css'> td.bgp { background-image: url('/cemqa/images/460x65_" + bg_Array[rand(bg_Array.length)] + ".jpg'); background-repeat: no-repeat; background-position: center center;} </style>");
// -->
</script>

If you can come up with something better, or have a suggestion on getting Netscape not to tile the td bg image, I'd love to hear it. Thanks.

"...there's some good in this world, Mr. Frodo, and it's worth fighting for." - Samwise Gamgee, The Two Towers

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives