Jump to content

help removing body.style.background;


meomike2000

Recommended Posts

i need to find a way to unset  the background color.

 

that said i have a script that does just that fine in firefox.

you can set a background image, or you can set a background color and hide the background image.

the problem is when i try to show the background image again.

 

this works fine in firefox,  i use this;

 background.style.color = ' '; 

 

i tried making the seting to transparent as well.....

 

this dont work in Internet Explorer, i find that after setting body.style.background = 'what ever color'; ,

i need to remove this body attribute but cant seem to find a way.

can anybody help.....

 

thanks in advance mike.....

 

 

Link to comment
https://forums.phpfreaks.com/topic/188423-help-removing-bodystylebackground/
Share on other sites

I don't think "background" is a standard object. You should be using "document.body.style.backgroundColor" and to reset the value you would use a null string not a string with a space. This test page works fine for me in IE and FF

 

<html>
<head>
<script type="text/javascript">

function setBackgroundColor(color)
{
    document.body.style.backgroundColor = color;
    return;
}

</script>
</head>
<body>

<button onclick="setBackgroundColor('#0000ff')">Blue</button><br />
<button onclick="setBackgroundColor('#ff0000')">Red</button><br />
<button onclick="setBackgroundColor('#00ff00')">Green</button><br />
<button onclick="setBackgroundColor('')">Reset</button><br />

</body>
</html>

i have set

var body = document.getElementById('body');

 

that is why i refer to it as body....

 

i have tried this as well

 

body.sytle.background = null;

 

and that fails as well.....

 

the problem is not setting or changing the background color,

the problem is i let user choose to set a background color or a background image.

 

both work fine except that after if a uset sets a background color then tries to set a background image it will not show the image, just the set color or just a white background.

if the image is set or set before a color all is good.

this problem only exist in ie......

 

thanks mike.

 

This works for me

 

<html>
<head>
<script type="text/javascript">

function setBackgroundColor(color)
{
    //remove current background setting
    document.body.style.background = '';
    document.body.style.backgroundColor = color;
    return;
}

function setBackgroundImage(imageURL)
{
    //remove current background setting
    document.body.style.background = '';
    document.body.style.background = "url("+imageURL+")"
    return;
}

</script>
</head>
<body>

<button onclick="setBackgroundColor('#0000ff')">Blue Backgrouond</button><br />
<button onclick="setBackgroundColor('#ff0000')">Red Backgrouond</button><br />
<button onclick="setBackgroundColor('#00ff00')">Green Backgrouond</button><br />
<button onclick="setBackgroundImage('image1.jpg')">Image 1 Background</button><br />
<button onclick="setBackgroundImage('image2.jpg')">Image 2 Background</button><br />
<button onclick="setBackgroundColor('')">Reset</button><br />

</body>
</html>

ok fixed, thanks for the help.....

 

the problem was when i was setting the background color i was doing it like this....

 

body.style.background = color;

 

that was the problem....

 

should have been body.style.backgroundColor......

 

that works...

 

thanks mike.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.