...

View Full Version : Set the bgcolor attribute value



Rain Lover
07-22-2011, 03:59 PM
Hi,

The following doesn't work:


<!DOCTYPE HTML>
<title>Sample flash</title>
<embed id="foo" src="http://www.w3schools.com/media/bookmark.swf" width="400" height="40" type="application/x-shockwave-flash" />

<script type="text/javascript">
document.getElementById("foo").setAttribute("bgcolor", "#00FF00");
</script>

But if you change setAttribute to ("width", "800"), it works with no problem! What am I missing?

Any help is appreciated!
Mike

Rowsdower!
07-22-2011, 04:22 PM
For any style changes in javascript you are best to use the .style approach:

<!DOCTYPE HTML>
<title>Sample flash</title>
<embed id="foo" src="http://www.w3schools.com/media/bookmark.swf" width="400" height="40" type="application/x-shockwave-flash" />

<script type="text/javascript">
document.getElementById("foo").style.backgroundColor="#00FF00";
</script>

Notice the camelcase for the style you are changing. Anything in CSS that has a hyphen in its name will be camelcase when changing it from javascript.

Philip M
07-22-2011, 04:25 PM
bgcolor is deprecated. Use the element's style object.


<div id = "foo">Some Stuff</div>

<script type = "text/javascript">
document.getElementById("foo").style.backgroundColor = "00FF00";
</script>

:eek: rowsdowner types faster than I do!

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Rain Lover
07-22-2011, 04:25 PM
For any style changes in javascript you are best to use the .style approach


Thanks for the answer, but they are two different things. To see what I mean please try adding bgcolor="#00FF00" to the embed code and compare it with changing the style background color.

Rain Lover
07-22-2011, 04:32 PM
bgcolor is deprecated. Use the element's style object.


<div id = "foo">Some Stuff</div>

<script type = "text/javascript">
document.getElementById("foo").style.backgroundColor = "00FF00";
</script>

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Dear Philip,

As far as my limited Knowledge serves me, bgcolor is widely used for flash files and it's valid:
http://kb2.adobe.com/cps/127/tn_12701.html

Besides, nothing can replace it and create the same effect. Please correct me if I'm mistaken.

Philip M
07-22-2011, 04:53 PM
Dear Philip,

As far as my limited Knowledge serves me, bgcolor is widely used for flash files and it's valid:
http://kb2.adobe.com/cps/127/tn_12701.html

Besides, nothing can replace it and create the same effect. Please correct me if I'm mistaken.

bgcolor is deprecated - obsolete - and may not be supported in future.
setAttribute has serious "issues" in IE and is only partially supported.
As suggested, you would be best advised to use the element's style object. Or you could use a css class.

Rain Lover
07-24-2011, 01:32 PM
Actually I'm building a widget in which users might choose a custom background color or set it to transparent.
Here's what I initially tried without the setAttribute method:


<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
<script type="text/javascript">
var x;
var y;
var z;
z = "#00FF00";
if (z == "transparent" || z == "") {
x = ""; y = "transparent";
}
else {
x = z; y = "window";
}
</script>
</head>
<body>
<embed id="foo" bgcolor="'+x+'" wmode="'+y+'" src="http://www.w3schools.com/media/bookmark.swf" width="400" height="40" type="application/x-shockwave-flash" />
</body>
</html>

But it didn't work. I wonder what I did wrong.
Thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum