Forum Moderators: open

Message Too Old, No Replies

Detecting when user changes text size display

Is there an event or environment variable for this?

         

StupidScript

12:35 am on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to set up a dynamic menu, fairly common, where there's a little plus sign that you click to expand a subtree.

Works great when MSIE is set to show text at "medium" size (or from the "normal" size, with Firefox), but it looks like absolute cr*p when the settings are changed to something larger or smaller.

Is there any JS indicator I can grab/observe when the user either changes the size of the text they view or when they already have it set to something other than medium/default?

Y'know ... when they have used or use the View menu and modify the Text Size ...

marek

1:51 am on Feb 5, 2005 (gmt 0)

10+ Year Member



You could try to guess actual font size from size of a text block, but wouldn't be easier to set dimensions of your menu in em? Then it should accommodate to any reasonable font size.

Rambo Tribble

2:42 am on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function findSize(){
if(document.body.currentStyle){
var sz=document.body.currentStyle.fontSize;
}else{
var vw=document.defaultView;
var cs=vw.getComputedStyle(document.body,'');
var sz=cs.getPropertyValue('font-size');
}
alert(sz);
}
</script>
</head>
<body>
<a href="#" onclick="findSize();">show size</a>
</body>
</html>

Note that different browsers return values in different units.

StupidScript

2:52 am on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Superior, Mr. Tribble!

I knew it would be you or Mr. Marx, in the end.

marek, I believe "em" refers to the size of a capital "M" in the chosen font, and as such is a "font" unit, not a "distance" unit. Similarly, "pt" should refer to font-size points (font, often translated for use as a screen unit), "px" should refer to screen pixel units (distance, usually translated by the browser for font use), "in" should refer to screen pixel units extrapolated to the Standard (American) unit system, and "cm" should refer to screen pixel units extrapolated to the Standard Metric unit system.

<edit>I must applaud the thought! (I meant to add something like that during the original posting ... = :o) </edit>

I stand ready for correction, however that's what 20 years of typesetting has infused in me. :)

[edited by: StupidScript at 3:14 am (utc) on Feb. 5, 2005]

Rambo Tribble

3:00 am on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are right about the value of an em, however, in the web context, ems can be used as units for the dimensions of block elements. If you use them in that capacity, the element so specified will scale when the parent element's font-size is changed. It should be noted that there are minor discrepencies between how various browsers interpret ems, but they are rarely a source of trouble unless your layout requires precise-to-the-pixel measurements.

Oh, and I'll leave that, ". . . in the end." bit to Mr. Marx.

StupidScript

3:22 am on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I continue to be delighted! ;)

<edit>Hmm ... getting "document.body has no properties" using FF-1.0 on RH9. I'll test it again when I get back to a Windows system at work, and try the "em" suggestion in the meantime.</edit>

Rambo Tribble

4:43 am on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Might need to give the body an ID and use document.getElementById('bodyID') to satisfy the strictest DOM interpretation. Never was all that good at "strict", but I keep trying.

My Xan/Win box is down (upgrades, thankfully, not repairs) so I can't speak for Linux-tic fringe. Wouldn't be a bit surprised if Konq/Safari is finicky, too.

Bernard Marx

11:43 am on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry lads. I've been asleep.

I remember we had a thread on this a good while back. The subject was "Can we tell when the user has changed the font size". It involved having an invisible text container and measuring its offsetWidth. Mine used a timer running in the background, and Rambo's test was triggered by document.mouseover. Then we argued bitterly about whether or not it is possible to change the browser text setting without firing a mouseover.

Rambo Tribble

2:46 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, yes, the vicious name-calling, the vituperous innuendo; quite rancorous, quite rancorous, indeed. I think, however, our goal in that case was to determine whether a given font-family resided on the target system. Why we didn't take a more direct approach escapes me, though.


function findFont(){
if(document.body.currentStyle){
var ff=document.body.currentStyle.fontFamily;
}else{
var vw=document.defaultView;
var cs=vw.getComputedStyle(document.body,'');
var ff=cs.getPropertyValue('font-family');
}
alert(ff);
}

Bernard Marx

7:25 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What's going on in the above, Rambo?

Rambo Tribble

12:57 am on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just demonstrating that it is easy enough to determine the actual font-family applied to an element, for assessment purposes. In the example I targeted the body, but that's just for example.

Bernard Marx

1:20 am on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For simplicity's sake, I've just done this one for IE:

<html>
<style>
body {font-family: "tribble sans serif"}
</style>
</head>
<body>
Here is some text.
<script>
alert(document.body.currentStyle.fontFamily)
</script>
</body>
</html>

If the purpose is to see whether or not a particular font is installed, then we've failed.
- or am I losing the plot?

Rambo Tribble

1:31 am on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that's why we went to the trouble of doing the comparison business. Thanks for the reminder. Not having messed with it for a while, I'd forgotten that the property regurgitates whatever it's fed, not what is actually applied.

StupidScript

3:10 am on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you, Mr. Tribble, for a sweet solution ... and to Mr. Marx for engaging in yet another interesting exchange.

I put up my test at the domain in my profile /expandingmenu.html, if you want to see it.

Now to figure out how to integrate it with the Exponent content management system ...

Onward! :)

Rambo Tribble

3:37 am on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I notice that you aren't using a DOCTYPE declaration on your page. You do realize that will force browsers into "quirks mode"? The biggest issue with quirks mode is that it makes it harder to get consistent results between browsers. Here's the W3C's page on the subject: [w3.org...]

The most forgiving variant is the transitional HTML 4.01:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

Be sure to use the whole thing; sometimes you will see the abbreviated form:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
but without the DTD URL, you're back to quirks mode.

StupidScript

3:51 am on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you ... annnnnnd ... fixed.

<edit>Annnnnd irritatingly, when I include the DTD's URL in the declaration, it breaks the code, dagnabbit! I'll work on it more ... </edit>

StupidScript

5:49 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yoiks! I just got a look at it on a Winbox ... yeeech! Don't look at it, yet! There's obviously work to do. :)

<edit>Ok, it's better ... absolute positioning for MSIE and relative for FF ... I'm still working on being able to include the URL in the DTD without breaking the style instructions on FF/Win/Lin. The page validates both as HTML 4.01 Transitional and as CSS, but there's clearly an issue with FF. Maybe I need to bump it up to "strict", and use CSS more ...?</edit>

StupidScript

3:20 am on Feb 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As-is ...

Works pretty well on Win MSIE and FF, and acceptable on Lin FF (bottom padding should be able to be a fixed quantity, i.e. 3px) ... it renders, but doesn't expand with Konqueror on Linux. Nor do the fonts render, so I'm not too concerned ... I'll make the menu links jump to the section homepage for Konqueror.

I'm still trying to figure out why it breaks when I reference the standard 4.01 Transitional DTD URL ... any ideas why the style or script breaks when that's included in the DTD? You can save it to your hdd and un-comment the URL in the DTD to test it ... ;)

(BTW: I use Konqueror ... I appreciate Konqueror ... but I figger Konqueror users are cut from a different cloth, and can handle a slightly-less-functional menu. :)

StupidScript

4:38 am on Feb 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the record (and to put this thread to bed):

Mr. Tribble's solution in Message #3 successfully picks up the font size according to the user's browser settings.

It needs an event, like a click or a load to fire it after the BODY element has initialized, but ... by golly, that's just fine.

Please PM me if you want to comment on the expanding menu code.