views:

40

answers:

2

I found some code at http://labnol.blogspot.com/2006/12/allow-site-visitors-to-change-font.html to allow folks to change font size and adapted it to use buttons.

<html>
<head>

<script language="JavaScript" type="text/javascript">
function changeFontSize(inc)
{
  var p = document.getElementsByTagName('*');
  for(n=0; n<p.length; n++) {
    if(p[n].style.fontSize) {
       var size = parseInt(p[n].style.fontSize.replace("px", ""));
    } else {
       var size = 16;
    }
    p[n].style.fontSize = size+inc + 'px';
   }
}
</script>

</head>
<body>
<basefont size=3/>
<p>
asdf
hhui
jnj
ghvt
</p>

<input type="button" value="+" onclick="changeFontSize(1)" />
<input type="button" value="-" onclick="changeFontSize(-1)" />

</body>
</html>

I am aware of browser features such as ctrl mouse wheel to change font size.

I have some problems with this...

1) How can I discover what the constant 16 should be rather than code it in? 2) Is there anyway to cause the font change to effect pages linked to by this page?

On another vein. If one uses something like the mouse wheel to change font size, my browser (Firefox) remembers the font size whenever the page is visited even in new executions of the browser. Are there other places accessible to java to set/discover this information?

A: 

Here's a JavaScript function you can use to calculate the size of 1em in pixels:

function getEmSize(el) {
    // If you pass in an element ID then get a reference to the element
    if (typeof el == "undefined") el = document.documentElement;

    var tempDiv = document.createElement("DIV"); 
    tempDiv.style.height = 1 + "em";
    // Just in case the content of "el" takes up it's container's height :)
    tempDiv.style.marginTop = -1 + "em";

    el.appendChild(tempDiv);
    var emSize = tempDiv.offsetHeight;
    el.removeChild(tempDiv);
    return emSize;
}

I also wrote an article about Changing Text Size On Click. Just make sure you save the preference via a cookie or localStorage.

Hope that's useful.

Eric Wendelin
I have a whole series of related pages. If I implement text size changes as discussed here there will be an expectation that the changes will persist between pages. (BTW, this is a good argument for this approach as opposed to browser features). If I save a cookie in one page, will I be able to retrieve it in another page? The pages will exist on the in the same directory on the server. Server belongs to my isp so can I expect problems when cookies are sent there?
Mike D
Yes, you can read a cookie as long as it is in the same top-level domain (e.g. google.com)
Eric Wendelin
Here's an awesome page with the whole shebang http://www.simon.kaulius.com/dynamically_change_text_javascript.htm
Mike D
A: 

The easiest thing to do is to change your page to not use pixels. If you used ems, all you would do is have to change the font size of the body element and everything else would adjust.

Most browsers have the ability to change the font size built in so coding this is really of no benefit if you code the page correctly to begin with.

epascarello
The ability to change the font size for a set of related pages is important to me. IMHO, I find the fact that the browser (Firefox) remembers that font size has been increased for a given page annoying.
Mike D