when do I need getPropertyValue()?

P

Peter Michaux

Hi,

Since I can do this

document.defaultView.getComputedStyle(el, null).height;

why would I ever want to do this

document.defaultView.getComputedStyle(el,
null).getPropertyValue('height');

Is there a browser with getComputedStyle() that cannot do the first way
and hence requires the use of getPropertyValue()?

Thank you,
Peter
 
V

VK

Peter said:
Hi,

Since I can do this

document.defaultView.getComputedStyle(el, null).height;

why would I ever want to do this

document.defaultView.getComputedStyle(el,
null).getPropertyValue('height');

The first syntax requires property name in camelCase; the second syntax
takes property name as it is. This way it could be possible to choose
the most convenient way for a particular situation w/o normalizing all
involved names. But as IE currentStyle takes only camelCase anyway, the
second syntax didn't go into wide use.
Also please note that as you are asking for *computed* (current) style,
you are getting normalized value back (how is it internally stored by
UA, so not necessarily how it is indicated in the source). For the
second reason the whole getComputedStyle interface is used only rather
occasionally for specific tasks - yet for these tasks it is very
useful.

// See the sample:

<html>
<head>
<title>Styles</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<style type="text/css">
body {
color: #000000;
background-color: #FFFFFF;
}
h1 {
background-color: yellow;
}
</style>
<script type="text/javascript">
function init() {
var header = document.getElementsByTagName('h1')[0];
if ((document.defaultView)
&& (document.defaultView.getComputedStyle)) {
window.alert(document.defaultView
.getComputedStyle(header,null).backgroundColor);
window.alert(document.defaultView
.getComputedStyle(header,null)
.getPropertyValue('background-color'));
}
else if (header.currentStyle) {
window.alert(header.currentStyle.backgroundColor);
}
}
window.onload = init;
</script>
</head>
<body>
<h1>Lorem ipsum</h1>
</body>
</html>
 
V

VK

VK said:
Also please note that as you are asking for *computed* (current) style,
you are getting normalized value back (how is it internally stored by
UA, so not necessarily how it is indicated in the source). For the
second reason the whole getComputedStyle interface is used only rather
occasionally for specific tasks - yet for these tasks it is very
useful.

To not be abstract: getComputedStyle can be used to determine the exact
pixel size of 1em for the given font on the given machine.
 
P

Peter Michaux

Hi VK,
The first syntax requires property name in camelCase; the second syntax
takes property name as it is. This way it could be possible to choose
the most convenient way for a particular situation w/o normalizing all
involved names. But as IE currentStyle takes only camelCase anyway, the
second syntax didn't go into wide use.

So other than hyphenated vs. camelCase is there any difference. Does
anyone know of a browser bug surrounding this issue where
getPropertyValue() is the only solution? I would much prefer to use the
first with it's camelCase property.

Thanks,
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top