By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

Opera and dynamic style change.. this must be a joke..

P: n/a
Ok, I tried simply everything that came to my mind and now I ran out
of ideas, but to the point - take a look at the code below

// GetStyle returns given style value (works fine)
document.getElementById("inner").style.backgroundC olor =
GetStyle("box", "backgroundColor");
document.getElementById("box").style.backgroundCol or = "transparent";

<div id="box" style="width: 100px; height: 100px; background-color:
Red">
<div id="inner" style="width: 50px; height: 50px"></div>
</div>

What this code do - it retrieves "background-color" style from "box"
element, assigns it to "inner" and then "box" background gets
transparent. Now the funny part - it works in every browser, except
Opera, where both elements gets transparent background. What's even
more funny, when I replace GetStyle(...) with string "Red" it works
fine (GetStyle also returns string). Can someone explain me what is
going on? It's so illogical that I'm starting to think that maybe this
is a bug in Opera..

Thanks for any help,
P.
Jun 27 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Apr 22, 12:52 am, Piotr K wrote:
Ok, I tried simply everything that came to my mind and now I ran
out of ideas, but to the point - take a look at the code below

// GetStyle returns given style value (works fine)
document.getElementById("inner").style.backgroundC olor =
GetStyle("box", "backgroundColor");
document.getElementById("box").style.backgroundCol or = "transparent";

<div id="box" style="width: 100px; height: 100px; background-color:
Red">
<div id="inner" style="width: 50px; height: 50px"></div>
</div>

What this code do - it retrieves "background-color" style
from "box" element, assigns it to "inner" and then "box"
background gets transparent. Now the funny part - it works
in every browser, except Opera, where both elements gets
transparent background. What's even more funny, when I
replace GetStyle(...) with string "Red" it works fine
(GetStyle also returns string). Can someone explain
me what is going on? It's so illogical that I'm starting
to think that maybe this is a bug in Opera..

Thanks for any help,
I don't know what help you expect. You have not included the code
necessary for anyone else to even attempt to reproduce your symptoms,
let alone attribute cause and effect relationships to them. And no
matter how effective you assert your - GetStyle - function to be I bet
most would like to see the code in question and verify its suitability/
reliability for themselves.
Jun 27 '08 #2

P: n/a
pr
Piotr K wrote:
Ok, I tried simply everything that came to my mind and now I ran out
of ideas, but to the point - take a look at the code below

// GetStyle returns given style value (works fine)
document.getElementById("inner").style.backgroundC olor =
GetStyle("box", "backgroundColor");
What is GetStyle()?
document.getElementById("box").style.backgroundCol or = "transparent";

<div id="box" style="width: 100px; height: 100px; background-color:
Red">
<div id="inner" style="width: 50px; height: 50px"></div>
</div>

What this code do - it retrieves "background-color" style from "box"
element, assigns it to "inner" and then "box" background gets
transparent. Now the funny part - it works in every browser, except
Opera, where both elements gets transparent background. What's even
more funny, when I replace GetStyle(...) with string "Red" it works
fine (GetStyle also returns string). Can someone explain me what is
going on? It's so illogical that I'm starting to think that maybe this
is a bug in Opera..
Maybe it is, maybe it isn't. Post the code for GetStyle() and we might
be able to tell.
Jun 27 '08 #3

P: n/a
On 22 Kwi, 12:21, pr <p...@porl.globalnet.co.ukwrote:
Piotr K wrote:
Ok, I tried simply everything that came to my mind and now I ran out
of ideas, but to the point - take a look at the code below
// GetStyle returns given style value (works fine)
document.getElementById("inner").style.backgroundC olor =
GetStyle("box", "backgroundColor");

What is GetStyle()?
document.getElementById("box").style.backgroundCol or = "transparent";
<div id="box" style="width: 100px; height: 100px; background-color:
Red">
<div id="inner" style="width: 50px; height: 50px"></div>
</div>
What this code do - it retrieves "background-color" style from "box"
element, assigns it to "inner" and then "box" background gets
transparent. Now the funny part - it works in every browser, except
Opera, where both elements gets transparent background. What's even
more funny, when I replace GetStyle(...) with string "Red" it works
fine (GetStyle also returns string). Can someone explain me what is
going on? It's so illogical that I'm starting to think that maybe this
is a bug in Opera..

Maybe it is, maybe it isn't. Post the code for GetStyle() and we might
be able to tell.
Here's the GetStyle code:

function GetStyle(obj, style) {
obj = document.getElementById(obj);

if(style == "opacity") return GetOpacity(obj);

if(obj.currentStyle) return obj.currentStyle[style];
else if(window.getComputedStyle) return getComputedStyle(obj,
"").getPropertyValue(style.replace(/[A-Z]/g, function(obj, ch) {return
"-"+style.charAt(ch).toLowerCase()}));
}

However I don't think this has anything to do with that strange
behaviour. In the example I posted earlier it returns simply "Red", so
in other words

GetStyle("box", "backgroundColor") == "Red"

When I use GetStyle, the background gets transparent under Opera, but
if I replace it with simply "Red" string everything works fine. I
thought there may be some kind of referencing going on under Opera,
but I tried even copying each character from GetStyle return value to
array and than joining it into completely new string - still the same
effect.
Jun 27 '08 #4

P: n/a
VK
On Apr 22, 3:52 am, Piotr K <piotr.korzeniew...@gmail.comwrote:
document.getElementById("box").style.backgroundCol or = "transparent";
"transparent" is an imaginary value. It was used in W3C docs to
describe an element that has no color property set, like "transparent
element", but it was not meant to be an actual string to assign to the
style. Now it is supported by some newer browsers due to misreading
some badly written W3C samples. Neither Opera not IE6 are one of them.

Never Ever In Your Life :-) put style rules to their default values by
any explicit assignment: because you never in your life can guess what
explicit assignment is expected by some particular UA (think of the
infamous table element display glitch). Always use empty string
instead: this way any UA will restore what it needs by itself.

document.getElementById('box').style.backgroundCol or = '';
Jun 27 '08 #5

P: n/a
pr
Piotr K wrote:
Here's the GetStyle code:

function GetStyle(obj, style) {
obj = document.getElementById(obj);

if(style == "opacity") return GetOpacity(obj);

if(obj.currentStyle) return obj.currentStyle[style];
else if(window.getComputedStyle) return getComputedStyle(obj,
"").getPropertyValue(style.replace(/[A-Z]/g, function(obj, ch) {return
"-"+style.charAt(ch).toLowerCase()}));
}

However I don't think this has anything to do with that strange
behaviour. In the example I posted earlier it returns simply "Red", so
in other words

GetStyle("box", "backgroundColor") == "Red"
Not quite. It appears in fact to return '"red"' - that is, a string
containing double quote marks, which is not a sensible input for
style.backgroundColor. Try this, for example:

alert("*" + GetStyle("box", "backgroundColor") + "*");

Opera doesn't do it after the value has been set in code to
'transparent', so you may have to do a bit of research to find out when
it occurs. My guess is when you use a named colour.

A troublesome one to spot.
Jun 27 '08 #6

P: n/a
On 22 Kwi, 18:21, pr <p...@porl.globalnet.co.ukwrote:
Piotr K wrote:
Here's the GetStyle code:
function GetStyle(obj, style) {
obj = document.getElementById(obj);
if(style == "opacity") return GetOpacity(obj);
if(obj.currentStyle) return obj.currentStyle[style];
else if(window.getComputedStyle) return getComputedStyle(obj,
"").getPropertyValue(style.replace(/[A-Z]/g, function(obj, ch) {return
"-"+style.charAt(ch).toLowerCase()}));
}
However I don't think this has anything to do with that strange
behaviour. In the example I posted earlier it returns simply "Red", so
in other words
GetStyle("box", "backgroundColor") == "Red"

Not quite. It appears in fact to return '"red"' - that is, a string
containing double quote marks, which is not a sensible input for
style.backgroundColor. Try this, for example:

alert("*" + GetStyle("box", "backgroundColor") + "*");

Opera doesn't do it after the value has been set in code to
'transparent', so you may have to do a bit of research to find out when
it occurs. My guess is when you use a named colour.

A troublesome one to spot.
You're absolutely right, all because of double quote marks returned by
GetStyle, I can't believe I missed that, thank you sooo much - now
everything works fine :)

Thanks for all responses!
Jun 27 '08 #7

P: n/a
VK wrote:
On Apr 22, 3:52 am, Piotr K <piotr.korzeniew...@gmail.comwrote:
>document.getElementById("box").style.backgroundCo lor = "transparent";

"transparent" is an imaginary value. It was used in W3C docs to describe
an element that has no color property set, like "transparent element",
but it was not meant to be an actual string to assign to the style.
Wrong on all accounts, see

http://www.w3.org/TR/CSS2/colors.htm...und-properties
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-58190037
http://www.w3.org/TR/DOM-Level-2-Sty...yleDeclaration
http://www.w3.org/TR/DOM-Level-2-Sty...CSS2Properties
Now it is supported by some newer browsers due to misreading some badly
written W3C samples. Neither Opera not IE6 are one of them.
Or maybe, just *maybe*, you are simply completely wrong *again* because both
user agents do support that string value. I guess it does not bother you to
be corrected all the time because a simple test -- like

var elemRef = document.body.getElementsByTagName("*")[0];
if (elemRef && typeof elemRef.style != "undefined")
{
elemRef.style.backgroundColor = "red";
window.alert("Now it should have a red background.");
elemRef.style.backgroundColor = "transparent";
window.alert("Now it should have a transparent background.");
}

-- before making further wild assumptions would have sufficed.

Nevertheless, your stating nonsense has been helpful this time as I could
find out that the `style' property and the shorthand CSS properties (for
example elemRef.style.backgroundColor) are in fact supported by W3C DOM
Level 2 CSS and _not_ a (fully) proprietary feature as I thought before.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #8

P: n/a
VK
On Apr 22, 10:52 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Wrong on all accounts, see

http://www.w3.org/TR/CSS2/colors.htm...und-properties
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-58190037
http://www.w3.org/TR/DOM-Level-2-Sty...yleDeclaration
http://www.w3.org/TR/DOM-Level-2-Sty...CSS2Properties
Some day I will maybe understand how anything written on W3C may force
to work it on browser X. Some day...
I guess it does not bother you to
be corrected all the time because a simple test -- like

var elemRef = document.body.getElementsByTagName("*")[0];
if (elemRef && typeof elemRef.style != "undefined")
{
elemRef.style.backgroundColor = "red";
window.alert("Now it should have a red background.");
elemRef.style.backgroundColor = "transparent";
window.alert("Now it should have a transparent background.");
}
So it works now, even on IE6? Great. I still highly suggest never use
direct assignments for rule reset: use "" instead. Nobody is obligated
to follow this advise of course.
Nevertheless, your stating nonsense has been helpful this time as I could
find out that the `style' property and the shorthand CSS properties (for
example elemRef.style.backgroundColor) are in fact supported by W3C DOM
Level 2 CSS and _not_ a (fully) proprietary feature as I thought before.
Since DOM 1 to be exact. But better later than never :-)
Jun 27 '08 #9

P: n/a
VK wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
>Wrong on all accounts, see

http://www.w3.org/TR/CSS2/colors.htm...und-properties
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-58190037
http://www.w3.org/TR/DOM-Level-2-Sty...yleDeclaration
http://www.w3.org/TR/DOM-Level-2-Sty...CSS2Properties

Some day I will maybe understand how anything written on W3C may force
to work it on browser X. Some day...
It is *implemented as specified*, Often Wrong.
>I guess it does not bother you to
be corrected all the time because a simple test -- like

var elemRef = document.body.getElementsByTagName("*")[0];
if (elemRef && typeof elemRef.style != "undefined")
{
elemRef.style.backgroundColor = "red";
window.alert("Now it should have a red background.");
elemRef.style.backgroundColor = "transparent";
window.alert("Now it should have a transparent background.");
}

So it works now, even on IE6?
Now? Even? It works with *all* IE versions that support the `style'
property, so since version 4.0 (documented and tested). Your attempts to
conceal your cluelessness are even more ridiculous than your clueless
boasting itself is.
Great. I still highly suggest never use direct assignments for rule reset:
use "" instead. Nobody is obligated to follow this advise of course.
I would hope so, because your advice does not present an equivalent solution
at all. Assigning the empty string resets the style property to its initial
value (which ISTM to be a proprietary feature, CMIIW), but the cascade may
cause the computed value of the property to be a completely different one.
>Nevertheless, your stating nonsense has been helpful this time as I could
find out that the `style' property and the shorthand CSS properties (for
example elemRef.style.backgroundColor) are in fact supported by W3C DOM
Level 2 CSS and _not_ a (fully) proprietary feature as I thought before.

Since DOM 1 to be exact. But better later than never :-)
Wrong again. W3C DOM Level 1 only reserved the `style' attribute of the
HTMLElement interface for future use, and it did not include a section on
CSS interfaces. Furthermore, W3C DOM Level 1 HTML is rendered (and marked)
obsolete by DOM Level 2 HTML since quite a while.

http://www.w3.org/TR/REC-DOM-Level-1...l#ID-011100101
http://www.w3.org/TR/DOM-Level-2-HTML/ ("Status of this document", "Note:")

Someone like you who continuously fails to get the basic facts right should
be *very* slow to boast. Probably someone has told you that before.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.