Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Help with getting fontSize on body tag

Reply
Thread Tools

Help with getting fontSize on body tag

 
 
Adam Risser
Guest
Posts: n/a
 
      06-08-2007
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/~wst...ng/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!

 
Reply With Quote
 
 
 
 
scripts.contact
Guest
Posts: n/a
 
      06-08-2007

Adam Risser wrote:
> 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/~wst...ng/mu/test.php


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

 
Reply With Quote
 
 
 
 
Adam Risser
Guest
Posts: n/a
 
      06-08-2007
On Jun 8, 3:45 pm, "scripts.contact" <(E-Mail Removed)>
wrote:
> Adam Risser wrote:
> > 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/~wst...ng/mu/test.php

>
> var body=document.body;
> var fs=
> (
> body.currentStyle||
> (window.getComputedStyle&&getComputedStyle(body,nu ll))||
> 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
Thanks again!

 
Reply With Quote
 
rf
Guest
Posts: n/a
 
      06-09-2007

"Adam Risser" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...

> Hi, I am writing a function to change the font-size


Ctrl mouse wheel?

--
Richard.


 
Reply With Quote
 
Adam Risser
Guest
Posts: n/a
 
      06-13-2007
On Jun 8, 11:02 pm, "rf" <(E-Mail Removed)> wrote:
> "Adam Risser" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ups.com...
>
> > Hi, I am writing a function to change the font-size

>
> Ctrl mouse wheel?
>
> --
> Richard.


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

 
Reply With Quote
 
dmark@cinsoft.net
Guest
Posts: n/a
 
      06-20-2007
On Jun 8, 3:55 pm, Adam Risser <(E-Mail Removed)> wrote:
> On Jun 8, 3:45 pm, "scripts.contact" <(E-Mail Removed)>
> wrote:
>
> > Adam Risser wrote:
> > > 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/~wst...ng/mu/test.php

>
> > var body=document.body;
> > var fs=
> > (
> > body.currentStyle||
> > (window.getComputedStyle&&getComputedStyle(body,nu ll))||
> > 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
> 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.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      06-20-2007
On Jun 13, 10:27 pm, Adam Risser <(E-Mail Removed)> wrote:
> On Jun 8, 11:02 pm, "rf" <(E-Mail Removed)> wrote:
>
> > "Adam Risser" <(E-Mail Removed)> wrote in message

>
> >news:(E-Mail Removed) oups.com...

>
> > > Hi, I am writing a function to change the font-size

>
> > Ctrl mouse wheel?

>
> > --
> > Richard.

>
> 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.


--
Rob

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
document.body.style.fontsize is not working jnag Javascript 4 05-31-2007 01:37 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
Getting SPAN fontSize attribute toddaa@gmail.com Javascript 1 11-01-2005 09:18 PM
HTML Literal Control & FontSize Veeresh ASP .Net 2 06-28-2004 07:08 PM
fontsize/stylesheet problem =?Utf-8?B?ZnJlZA==?= ASP .Net 0 06-09-2004 08:56 AM



Advertisments