Help with getting fontSize on body tag

A

Adam Risser

Hi, I am writing a function to change the font-size but I am running
into a snag. I cannot get the current font-size of the body. Here is
a link to a simplified test case

http://mustang.millersville.edu/~wstudent/cewing/mu/test.php

You would think that it would alert 72.5% when you click the resize
button, but it alerts nothing. If i add the style to the body tag
(style="font-size: 72.5%;" ), then it works.

Here is the test case code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html;
charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<script type="text/javascript">

function resizeText()
{
alert(document.getElementsByTagName('body')[0].style.fontSize);
}

</script>
<style type="text/css">

body {
background: transparent url('../img/bg_pattern.gif') repeat 0 0;
font-size: 72.5%;
font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif
}

</style>
</head>
<body>

<p>Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test Test Test Test</p>

<p>Text Resize&nbsp; <a href="#" id="grow"
onclick="resizeText()">A<sup>+</sup></a> </p>

</body>
</html>

Thanks in advance!
 
A

Adam Risser

var body=document.body;
var fs=
(
body.currentStyle||
(window.getComputedStyle&&getComputedStyle(body,null))||
body.style
).fontSize;
alert(fs);


Thank you for your help. When I put that in an onload function, it
returns the size in pixels.
I am looking hopefully to just return whatever font-size is set in the
css. (In this case, 72.5%)

I have no real idea how that snippet works, but it is interesting :p
Thanks again!
 
D

dmark

Thank you for your help. When I put that in an onload function, it
returns the size in pixels.
I am looking hopefully to just return whatever font-size is set in the
css. (In this case, 72.5%)

I have no real idea how that snippet works, but it is interesting :p
Thanks again!

No dice unless you use an inline style attribute on the body tag (not
a good idea.) Regardless, this is a waste of time; leave the font
sizing to the user agent.
 
R

RobG

I am looking to have A+ and A- buttons on the site so people can
change the font who do not know about Ctrl mouse wheel

Most will tell not to try, users can change the font size themselves a
number of ways. On a 33cm screen at 1280x800px I have the default
font set to 18pt, on a 48cm screen at 1280x1024px I have it set to
14pt. I can use ctrl+"plus" to make it bigger at any time I like, ctrl
+"minus" to make it smaller.

But since you wont heed that advice, use CSS and modify a style rule
that sets the body's font size in percentage. Some browsers will
report the font size in whatever units were used to set it, others
will report in whatever unit they wish, regardles of what was used to
set it. You will need to test thoroughly.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top