I cannot get the colour of an html element using javascript until after it has been set in the script. For example:
<html>
<head>
<style media="screen" type="text/css">
h1 {
color: purple;
}
</style>
<script type="text/javascript">
function test() {
var header = document.getElementsByTagName("h1")[0];
alert("Colour: " + header.style.color); // Does not get the correct colour
header.style.color = "yellow";
alert("Colour: " + header.style.color); // Gets the correct colour
}
</script>
</head>
<body onload="test();">
<h1>Header</h1>
</body>
</html>
I would expect the first alert to show "Colour: purple" as set in the css and the second to show "Colour: yellow" from the function itself. However, the first alert simply shows "Colour: ". In both cases, the colour of the element is correct and verified using firebug.