I'm so defeated I'm prepared to suffer any abuse to find out why I can't do something as simple as get the onload function to tell me the body font size is 20pt. I've read till my eyes bleed, and everyone but me seems able to make the script below work.
But my #@!/%#! code vomits an alert box with the following:(I used two lines to be sure it wasn't a case problem)
[ ]
[undefined]
Actually, my goal was to use scritp that executed in the head section, or onload, to read the screen width, and set the body font size by formula. [Next try will be to leave the body tag out of the code, and have the script document.write() in a body tag with style specs that sets the body font size. This below was supposed to just be a warm up exercise.] Any and all help is appreciated.
<html>
<head>
<style>
body {font-size:20pt;color:white;font-weight:bold;background:grey;}
</style>
<script type="text/javascript">
onload=function(){
alert("[ " + document.getElementsByTagName("body")[0].style.fontSize +" ]"
+"\n[ " + document.getElementsByTagName("body")[0].style.fontsize + " ]");
}
</script>
</head>
<body>
<div>Test Text</div>
</body>
</html>
The following s is as far as I got with the help of comments below.
These both return font size.
getComputedStyle(document.body).fontSize
getComputedStyle(document.body).getPropertyValue('font-size')
But they throw errors when used to set font size
getComputedStyle(document.body).fontSize = "30pt";
getComputedStyle(document.body).setProperty("fontSize", "25pt", "important") ;
On the other hand, these both set new font size
document.body.style.fontSize = "200%";
document.getElementsByTagName("body")[0].style.fontSize = "400%";
But they both return a string of lenght 0 when asked for font size.
document.body.style.fontSize
document.getElementsByTagName("body")[0].style.fontSize
Here's an onload function to confirm it with:
onload=function(){
var msg = "onload:\n";
/* these both return font size.*/
msg += "1. [ " + getComputedStyle(document.body).fontSize + "]\n";
msg += "2. [ " + getComputedStyle(document.body).getPropertyValue('font-size') + "]\n";
/* these both return a string of lenght 0.*/
msg += "3. [ " + document.body.style.fontSize + "]\n";
msg += "4. [ " + document.getElementsByTagName("body")[0].style.fontSize + "]\n";
/*these both throw errors*/
try { getComputedStyle(document.body).fontSize = "30pt";
} catch (err) { msg += "5. [" + err.message + "]\n"; }
try {getComputedStyle(document.body).setProperty("fontSize", "25pt", "important") ;
} catch (err) { msg += "6. [" + err.message + "]\n"; }
/*these both set new font size*/
alert(msg);
document.body.style.fontSize = "200%";
setTimeout(function(){document.getElementsByTagName("body")[0].style.fontSize = "400%"},1500);
}
There is obviously something I'm missing, but for now this works. Merry Christmas