Click to See Complete Forum and Search --> : Javascript + CSS?


Odoacer
01-24-2003, 03:01 PM
not sure if this should be in the javascript or css forum...

okay well i've got a javascript random background chooser and i need to set the background using CSS styles.


My CSS code is nothing special:
body {background:ffffff url(JAVASCRIPT HERE) no-repeat fixed right top }

What i need is a way for the url() function in CSS to take use the picture that JavaScript chooses.


ok i simply made a javascript function (printImage) that prints out the name of the selected picture. I can't figure out how to implement it in CSS - url(printImage()) doesnt work... because <script> and <style> tags dont seem to work nested inside each other.

any workarounds/ideas? help.... thanks

BestZest
01-24-2003, 03:08 PM
I guess you would need to do:

function changeBackground() {
document.style.background="url(\""+chosenImg+"\")"
}

Don't know if this works,

BestZest

Odoacer
01-24-2003, 04:43 PM
document.style isnt valid... and the netscape javascript documentation doesn't really provide complete examples so im kinda lost

Will i still be able to set the position and tiling of the backgroud if i do it this way?

khalidali63
01-24-2003, 09:52 PM
You can get the required effect by using the following code.


<script type="text/javascript">
window.onload=printImage;
function printImage(){
var iurl = "images/flake.gif"; document.getElementById("parent").style.background="url("+iurl+")";
}

</script>
</head>

<body id="parent">


cheers

Khalid

Odoacer
01-24-2003, 11:15 PM
thanks, it works but is there any way to make it "no-repeat fixed right top" a'la CSS

khalidali63
01-25-2003, 12:51 AM
make your css like this
#parent{
background:ffffff url(JAVASCRIPT HERE) no-repeat fixed right top
}

and then the onload code should take over and put the url u want in it.

did not test it myself,but should work